javascript - 将事件绑定(bind)到 ajax 加载的内容

标签 javascript jquery ajax

我使用此代码片段在单击链接后显示微调器。有时链接.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");
};

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

最佳答案

尝试

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

关于javascript - 将事件绑定(bind)到 ajax 加载的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20135229/

相关文章:

javascript - 如何将多个 id 大写(使用 javascript 的 php 中的复选框)

javascript - 删除以特定字符开头的字符串

javascript - OnClick 函数不会在 iframe 上触发?

javascript - 同步等待处理请求

javascript - 如何进行可重用的 proptype 验证?

javascript - Strapi isOwner 政策实现

javascript - 为什么我用php生成的图像只生成一次,由jquery调用

javascript - jQuery/Ajax 成功方法实现

php - 我正在失去错误与 Symfony2 中表单字段的关联

javascript - 使用 PHP 和 Ajax 将文件上传到 Google Cloud Storage Bucket