jQuery live()是否可用于removeAttr()? [英] jQuery live() for removeAttr()?

查看:74
本文介绍了jQuery live()是否可用于removeAttr()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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()?

注意:我无法控制正在进行ajax调用的JavaScript库.

推荐答案

此操作会为当前和将来具有不良属性"的所有元素创建一个新事件,接下来我们将触发它触发并完成其工作

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");
});

关于成功的ajax调用功能

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");
});

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

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");
});

这篇关于jQuery live()是否可用于removeAttr()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆