jQuery live() 用于removeAttr()?
我需要对可以通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这会为现在和将来具有“不良属性”的所有元素创建一个新事件,接下来我们将触发它并执行其工作。
在成功的 ajax 调用函数上,
如果您无法控制所有 ajax,则必须依赖导致 ajax 触发的用户事件......这很脏:
//您认为导致无法控制的ajax触发的事件,例如更改
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.
on the successfull ajax call's function
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
您可以使用
$.ajax
的complete
选项 请求如下:You can use
complete
option of the$.ajax
request like this:您要寻找的是在加载这些元素时处理此问题,这将位于 AJAX 调用的
success
回调中:更新:如果您不这样做如果您无法控制进行 AJAX 调用的任何内容,并且它不提供任何挂钩或回调,您将需要找到另一个事件来绑定才能执行此操作。根据这些元素插入页面的方式以及您正在对它们执行的操作,您也许可以使用 delegate 像这样(只是猜测):
我不知道修改 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: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):
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.