jQuery.live()在插件内部不起作用 [英] jQuery.live() not working inside of a plugin
本文介绍了jQuery.live()在插件内部不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个插件,需要实时绑定一个点击.当我执行普通的单击绑定而不是实时绑定时,该插件可以正常工作.
I am writing a plugin and need to live bind a click. The plugin works fine when I do a normal click bind, but not a live bind.
我已将插件归结为基础知识:
I've boiled the plugin down to the basics:
(function($) {
$.fn.liveBindTest = function() {
return this.each(function() {
$(this).live('click', function(){
console.log('live click');
return false;
});
$(this).click(function(){
console.log('click');
return false;
});
});
};
})(jQuery);
当我在链接上调用插件函数时,只有click
打印到我的控制台上.
When I call the plugin function on a link, only click
is printed to my console.
要使live()
正常工作,我该怎么做?谢谢.
What must I do in order for live()
to work? Thanks.
推荐答案
仔细考虑之后,我意识到在现有DOM元素上调用live
是没有意义的,因为它已经在DOM中.
After thinking this through, I realized it makes no sense to call live
on an existing DOM element because it's already in the DOM.
技巧是在调用插件时使用live
:
Trick is to use live
when invoking the plugin:
(function($) {
$.fn.liveBindTest = function() {
return this.each(function() {
$(this).click(function(){
console.log('click');
return false;
});
});
};
})(jQuery);
$('a').live('click', function(){ $(this).liveBindTest(); });
这篇关于jQuery.live()在插件内部不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文