jQuery&;livequery-检查函数是否已附加到元素
我正在运行这个:jQuery&;livequery-检查函数是否已附加到元素,jquery,livequery,Jquery,Livequery,我正在运行这个: $("*:not(#boo) .content").livequery(function(){ $("input, select").blah(); }); blah()如下所示: (function( $ ) { $.fn.blah = function(){ var that = this; return this.each(function(){ $(this).bind('change', function(){
$("*:not(#boo) .content").livequery(function(){
$("input, select").blah();
});
blah()
如下所示:
(function( $ ) {
$.fn.blah = function(){
var that = this;
return this.each(function(){
$(this).bind('change', function(){
// do some stuff here
return true;
}).change();
});
};
})(jQuery);
html看起来像:
<div id="boo">
<div class="content">
<input type="text" />
<input type="text" />
...
</div>
</div>
<div class="content">
<input type="text" />
<input type="text" />
</div>
...
...
...
所以我要做的是将该函数和事件附加到不在#boo
中的每个输入元素。
这是可行的,但问题是每秒钟重复一次,浏览器就会冻结
我需要livequery,因为html有时会更新,我需要将事件再次附加到新元素
那么,我如何检查
blah()
是否已经应用于输入元素并停止呢?老实说,我不会这样做。您的第一个语句是一个大NoNo,它查询标记中的每个节点,然后排除您需要的元素。为什么不这样做:
(function( $ ) {
$.fn.blah = function(){
var that = this;
return this.each(function(){
$(this).bind('change', function(){
// do some stuff here
return true;
}).change();
});
};
})(jQuery);
这当然是有意义的,如果没有更多的事情要做,这超出了我的范围。但看起来您甚至不需要这里的.livequery
更新
上面的代码无法工作。但这应该做到:
$('input').live('change', function() {
if(!$(this).closest('#boo').length) {
// do some stuff here
}
}).change();
当您查询
$.data(对象“事件”)
时,您会得到一个对象,该对象附带了事件的属性。因此,在您的案例中,您可以添加以下条件:
(function( $ ) {
$.fn.blah = function(){
var that = this;
return this.each(function(){
if ($.data( $(this).get(0), 'events' ) !== void(0) &&
$.data( $(this).get(0), 'events' ).change === void(0)) {
$(this).bind('change', function(){
// do some stuff here
return true;
}).change();
}
});
};
})(jQuery);
…以便只绑定尚未绑定的函数。因为她只需要那些不在#boo中的函数,所以我认为应该只有一个
代码>在您的筛选函数中。。。不管怎样,+1