单击“事件仅在第一次 jQuery 工作”



下面的代码在刷新页面后首次工作。但是当我尝试再次单击时,它不起作用。

单击

元素后,它会转到下一页(详细信息页面(,当它返回时,单击事件不起作用。

getNewsCategoryloop: function(dataMap){
    var _this = this;
    var text;
    text  = "<div id="repeat_get_list_of_category">";
    for(key in dataMap) {
        text += '<div class="gallery_wrap col-2">
                    <div class="gallery_view full_image" id="cat'+key+'">
                        <img src="assets/img/news/athletics_img.jpg" alt="athletics">
                        <span class="gallery_title">'+ (dataMap[key].field_saf == null ? 'no title' : dataMap[key].field_saf) +'</span>
                    </div>
                </div>';
                $(document).on('click', "#cat"+key, function(){
                    _this.getNewsbyCategoryName("#cat"+key);
                })
    }
 text+= "</div>";
 $("#getListOfNewsCategorylist").html(text);            
},  

输入此代码:-

$(document).on('click', "#cat"+key, function(){
  _this.getNewsbyCategoryName("#cat"+key);
})

在整个功能本身之外,并像下面这样更改它:-

$(document).on('click', ".gallery_view", function(){
  app.getNewsbyCategoryName($(this).attr('id'));
})

所以代码需要:-

getNewsCategoryloop: function(dataMap){
    var _this = this;
    var text;
    text  = "<div id="repeat_get_list_of_category">";
    for(key in dataMap) {
        text += '<div class="gallery_wrap col-2">
                    <div class="gallery_view full_image" id="cat'+key+'">
                        <img src="assets/img/news/athletics_img.jpg" alt="athletics">
                        <span class="gallery_title">'+ (dataMap[key].field_saf == null ? 'no title' : dataMap[key].field_saf) +'</span>
                    </div>
                </div>';
    }
    text+= "</div>";
    $("#getListOfNewsCategorylist").html(text);         
};
$(document).on('click', ".gallery_view", function(){
   app.getNewsbyCategoryName($(this).attr('id'));
})

最新更新