单击事件仅在第一次运行 jquery

click event working only for the first time 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'));
})