如何从 javascript 小部件中删除所有 jquery 实时事件

发布于 2024-12-22 03:52:11 字数 248 浏览 5 评论 0原文

我正在开发一些可以在许多其他网站上加载的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

这样的小城市 2024-12-29 03:52:11

执行此操作的简单方法是与命名空间绑定,并删除所有命名空间事件:

$('.my-submit').live('click.myPlugin', function() {...});

然后您可以调用 die 与命名空间:

$('.my-submit').die('click.myPlugin'); // only myPlugin events are removed

更好的是使用 on off jQuery 1.7 中引入的功能。这是处理绑定和解除绑定事件的一种更好的方法:

$(document).on('click.myPlugin', '.my-submit', function() {...}))
           .on('click.myPlugin', '.my-link', function() {...}));

$(document).off('click.myPlugin'); // remove all myPlugin functions

The simple way to do this is to bind with a namespace, and remove all the namespaced events:

$('.my-submit').live('click.myPlugin', function() {...});

You can then call die with the namespace:

$('.my-submit').die('click.myPlugin'); // only myPlugin events are removed

Better yet is to use the on and off functionality introduced in jQuery 1.7. This is a far superior way to handle binding and unbinding events:

$(document).on('click.myPlugin', '.my-submit', function() {...}))
           .on('click.myPlugin', '.my-link', function() {...}));

$(document).off('click.myPlugin'); // remove all myPlugin functions
浴红衣 2024-12-29 03:52:11

从 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.

尹雨沫 2024-12-29 03:52:11

您想使用 .die()

从之前使用 .live() 附加的所有事件处理程序中删除
元素。

来源:http://api.jquery.com/die/

旁注,您应该从使用 .live() 转向 .delegate() (或 .on() 取决于您的版本jQuery),因为它会提高性能,并且 .live() 是从 jQuery 1.7 开始已弃用。

$(<selector>).live(<event>, <handler>);

与以下内容相同:

$(document).delegate(<selector>, <event>, <handler>);

使用 .delegate() 的好处是您可以选择根元素(不必强制使用 document)。

给你的一些文档:

You want to use .die():

Remove all event handlers previously attached using .live() from the
elements.

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.

$(<selector>).live(<event>, <handler>);

is the same as:

$(document).delegate(<selector>, <event>, <handler>);

The bonus to using .delegate() is that you can choose your root-element (you aren't forced to use document).

Some docs for ya:

单挑你×的.吻 2024-12-29 03:52:11

您可以尝试 JQuery.unbind() 方法。您可以像这样使用它:

$.('.my-link').unbind('click');

希望这会有所帮助。

You could try the JQuery.unbind() method. You would use it like this:

$.('.my-link').unbind('click');

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文