This question already has answers here:
Event binding on dynamically created elements?
                                
                                    (23个答案)
                                
                        
                5年前关闭。
            
        

单击链接后,我将使用此代码片段显示微调器。有时,链接.ln-edit-task是通过Ajax加载的,代码段中断了。如何使其在Ajax加载的内容上起作用?

jQuery(document).on("ready page:change", function() {
  return $(".ln-destroy-task").on("confirm:complete", function(e, response) {
    if (response) {
      return showSpinner($(this));
    }
  });
});
showSpinner = function(el) {
  return el.find(".fa").removeClass().addClass("fa fa-spinner fa-spin");
};


请注意,Rails中的Turbolinks需要page:change

最佳答案

尝试

$(document).on("confirm:complete", ".ln-destroy-task", function (e, response) {
    if (response) {
        return showSpinner($(this));
    }
});

10-06 00:12