如何从 javascript 小部件中删除所有 jquery 实时事件
我正在开发一些可以在许多其他网站上加载的 javascript 小部件。
当小部件加载时,我在链接和按钮上绑定 jquery 实时事件。 例如:
$('.my-submit').live('click', function() {...});
...
$('.my-link').live('click', function() {...});
所以问题是如何仅删除小部件链接和按钮的所有实时事件?
I'm developing some javascript widget which loaded on many other sites.
When widget are loading I bind jquery live event on links and buttons.
For instance:
$('.my-submit').live('click', function() {...});
...
$('.my-link').live('click', function() {...});
So the question is how can I remove all live events only for widget links and buttons?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
执行此操作的简单方法是与命名空间绑定,并删除所有命名空间事件:
然后您可以调用
die
与命名空间:更好的是使用
on
和off
jQuery 1.7 中引入的功能。这是处理绑定和解除绑定事件的一种更好的方法:The simple way to do this is to bind with a namespace, and remove all the namespaced events:
You can then call
die
with the namespace:Better yet is to use the
on
andoff
functionality introduced in jQuery 1.7. This is a far superior way to handle binding and unbinding events:从 jQuery 1.7 开始,.live() 方法已被弃用。使用 .on() 附加事件处理程序。旧版本 jQuery 的用户应优先使用 .delegate() 而不是 .live()。
参考 jQuery 本身:
从 jQuery
使用 on 或 delegate,这不是问题。
As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().
Point of reference jQuery themselves:
From jQuery
Use on or delegate and this is not an issue.
您想使用
.die()
:来源:http://api.jquery.com/die/
旁注,您应该从使用
.live()
转向.delegate()
(或.on()
取决于您的版本jQuery),因为它会提高性能,并且.live()
是从 jQuery 1.7 开始已弃用。与以下内容相同:
使用
.delegate()
的好处是您可以选择根元素(不必强制使用document
)。给你的一些文档:
.die()
: http://api.jquery.com/ die.delegate()
: http://api.jquery.com/委托You want to use
.die()
:Source: http://api.jquery.com/die/
On a side-note, you should move from using
.live()
to.delegate()
(or.on()
depending on your version of jQuery) because it will improve performance and.live()
is deprecated as of jQuery 1.7.is the same as:
The bonus to using
.delegate()
is that you can choose your root-element (you aren't forced to usedocument
).Some docs for ya:
.die()
: http://api.jquery.com/die.delegate()
: http://api.jquery.com/delegate您可以尝试 JQuery.unbind() 方法。您可以像这样使用它:
希望这会有所帮助。
You could try the JQuery.unbind() method. You would use it like this:
Hope this helps.