jQuery live() 用于removeAttr()?

发布于 2024-10-15 15:16:44 字数 139 浏览 1 评论 0原文

我需要对可以通过 ajax 加载的元素使用removeAttr。有没有一种方法可以自动执行此操作,类似于使用 live() 自动绑定事件的方法?

注意:我无法控制执行 ajax 调用的 JavaScript 库。

I need to use removeAttr on elements that may be loaded via ajax. Is there a way to automatically do this, similar to the way you can bind events automatically with live()?

NOTE: I don't have control over the JavaScript libraries that are doing the ajax calls.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蹲在坟头点根烟 2024-10-22 15:16:44

这会为现在和将来具有“不良属性”的所有元素创建一个新事件,接下来我们将触发它并执行其工作。

$("mySelector").live("myRemoveAttrEvent", function(event){
     $(this).removeAttr("myAttr");
});

在成功的 ajax 调用函数上,

// quick jQ ajax, the important part is on success
$("div").load("url", function(data, status, xhr){
   ..do work..
// this is the important part
   $("mySelector").trigger("myRemoveAttrEvent");
});

如果您无法控制所有 ajax,则必须依赖导致 ajax 触发的用户事件......这很脏:
//您认为导致无法控制的ajax触发的事件,例如更改

$("*").change()(function(event){
       $("mySelector").trigger("myRemoveAttrEvent");
});

this creates a new event for all elements now and in the future that have your 'undesirable attribute', next we'll trigger it to fire and do its work.

$("mySelector").live("myRemoveAttrEvent", function(event){
     $(this).removeAttr("myAttr");
});

on the successfull ajax call's function

// quick jQ ajax, the important part is on success
$("div").load("url", function(data, status, xhr){
   ..do work..
// this is the important part
   $("mySelector").trigger("myRemoveAttrEvent");
});

if you do not have control over all the ajax, you have to piggy back on the user events that Cause the ajax to fire ... this is dirty:
//events you think cause the uncontrollable ajax to fire, e.g change

$("*").change()(function(event){
       $("mySelector").trigger("myRemoveAttrEvent");
});
痴者 2024-10-22 15:16:44

您可以使用 $.ajaxcomplete 选项 请求如下:

$.ajax({
  ......
  complete:function(){
    $('selector').removeAttr('attribute here');
  }
});

You can use complete option of the $.ajax request like this:

$.ajax({
  ......
  complete:function(){
    $('selector').removeAttr('attribute here');
  }
});
情定在深秋 2024-10-22 15:16:44

您要寻找的是在加载这些元素时处理此问题,这将位于 AJAX 调用的 success 回调中:

$.ajax({
  // your details
  success: function(html){
    $('a', html).removeAttr('title');
    $('body').append(html);
  }
});

更新:如果您不这样做如果您无法控制进行 AJAX 调用的任何内容,并且它不提供任何挂钩或回调,您将需要找到另一个事件来绑定才能执行此操作。根据这些元素插入页面的方式以及您正在对它们执行的操作,您也许可以使用 delegate 像这样(只是猜测):

$('body').delegate('p', 'load', function(){ /* remove attr */ });

我不知道修改 DOM 或单个元素时会触发任何事件。您可以尝试 load,但我认为在 AJAX 加载和插入元素的情况下它不会被调用。

What you're looking for is to handle this at the time those elements are loaded, which would be in the success callback for your AJAX call:

$.ajax({
  // your details
  success: function(html){
    $('a', html).removeAttr('title');
    $('body').append(html);
  }
});

Update: If you don't have control of whatever is making the AJAX calls and it doesn't provide any hooks or callbacks, you are going to need to find another event to bind to in order to perform this action. Depending on how these elements are being inserted into the page and exactly what you're doing with them, you might be able to use delegate like this (just a guess):

$('body').delegate('p', 'load', function(){ /* remove attr */ });

I don't know of any events that are triggered when the DOM or a single element is modified. You can try load, but I don't think it gets called in the case of AJAX loaded and inserted elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文