jQuery关于解除绑定的问题

发布于 2024-11-26 07:21:32 字数 337 浏览 1 评论 0 原文

我可以将 jQuery unbind() 与 live() 一起使用吗

,例如

$(".content_media").unbind("touchstart").live("touchstart",function(){....});

如果可以,它到底是什么意思?基本上我想了解解除绑定意味着什么

我有一个页面,我在文档上绑定元素准备就绪...并且在其之间有 AJAX 调用,它重写了相同的元素...现在我希望它们的行为完全相似次..这就是我使用 live() 的原因

如果这里可能存在一些例外情况,其中 live() 绑定不起作用,请纠正我。

Can i use jQuery unbind() with live()

e.g.

$(".content_media").unbind("touchstart").live("touchstart",function(){....});

If yes, what exactly does it mean? Basically I want to understand what does an unbind mean

I have a page where I bind elements on document ready...and in between there are AJAX callls which kind of rewrite the same elements...Now I expect them to behave similarly at all times..which is why I have used live()

Please correct me if there can be some exceptions here where the live() bindings won't work..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

相思碎 2024-12-03 07:21:32

是的,您可以使用 unbind()Doc< /a> 与 live()Doc如图所示。

此代码:

$(".content_media").unbind("touchstart").live("touchstart",function(){...

取消绑定通过 jQuery bind() 设置的任何 touchstart 事件侦听器,然后创建 touchstart 侦听器对于当前和任何未来节点,具有 content_media 类。

请注意,要停止使用 live() 设置侦听器,您需要使用 die()文档

此外,在 jQuery 外部设置的侦听器并不总是受到这些方法的影响。

Yes, you can use unbind()Doc with live()Doc as shown.

This code:

$(".content_media").unbind("touchstart").live("touchstart",function(){...

Unbinds any touchstart event-listeners that were set via jQuery bind(), then creates touchstart listeners for the current, and any future nodes with the content_media class.

Note that to stop listeners being set with live() you need to use die()Doc.

Also, listeners set outside jQuery are not always affected by these means.

北方。的韩爷 2024-12-03 07:21:32

取消绑定意味着从一组元素中删除事件处理程序。你可以只删除一些事件处理程序

$(".content_media").unbind("click");
//it means delete all click handlers on elements with class content_media

或所有事件处理程序

$(".content_media").unbind()
//it means delete all handlers on elements with class content_media

你可以在解除绑定后绑定新的实时事件,live()的注意事项是这样的(摘自文档)

因为 .live() 方法通过以下方式将事件处理程序绑定到文档
默认情况下,在文档上调用 .unbind() 将取消绑定处理程序绑定
也可以通过 .live() 实现。例如,$(document).unbind('click');将要
不仅删除 $(document).bind('click', fn1) 还删除
$('a.foo').live('click', fn2).

Unbind means removing event handlers from a set of elemnts. you can remove only some event handlers

$(".content_media").unbind("click");
//it means delete all click handlers on elements with class content_media

or all event handlers

$(".content_media").unbind()
//it means delete all handlers on elements with class content_media

You can bind new live events after unbinding them, the attention with live() is this (taken from documentation)

Because the .live() method binds event handlers to document by
default, calling .unbind() on document will unbind the handlers bound
by .live(), as well. For example, $(document).unbind('click'); will
remove not only $(document).bind('click', fn1) but also
$('a.foo').live('click', fn2).

有木有妳兜一样 2024-12-03 07:21:32

取消绑定意味着删除之前附加到元素的事件处理程序:

从元素中删除先前附加的事件处理程序。

但是您不能使用 unbind 删除使用 live 添加的事件处理程序,因为 live 的工作方式不同(它将事件处理程序附加到文档根目录)。

这就是 .die() 的用途:

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

To unbind means to remove the event handlers you previously attached to an element:

Remove a previously-attached event handler from the elements.

But you cannot use unbind to remove an event handler added with live because live works differently (it attaches the event handler to the document root).

That's what .die() is for:

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

绝對不後悔。 2024-12-03 07:21:32

我不明白你在这里解除绑定的意义是什么..

$(".content_media").unbind("touchstart").live("touchstart",function(){....});

好吧,如果你有函数 fn1 然后你有它,

$(".content_media").live("touchstart",fn1);

那么在某个时刻,你想使用 fn2 code> 没有 fn1 之类的,

$(".content_media").live("touchstart",fn2);

那么你最好先调用 unbind...以删除 fn1

$(".content_media").unbind("touchstart").live("touchstart",fn2);

但如果你只想要 fn1 那么你就不需要不必使用解除绑定实际上...

编辑

如果你这样做的原因是因为你

$(".content_media").unbind("touchstart").live("touchstart",function(){....});

在每次ajax执行中调用这一行...那么这就证明了它的合理性...
但你做错了......

只需将它放在ajax执行之外并且不取消绑定,那么你就可以开始......

$(".content_media").live("touchstart",function(){....});

I don't get what's your point for unbind here..

$(".content_media").unbind("touchstart").live("touchstart",function(){....});

well, if you have function fn1 and then you have it as,

$(".content_media").live("touchstart",fn1);

then at a certain point, you want to use fn2 without fn1 as like,

$(".content_media").live("touchstart",fn2);

then your good to call unbind first... to remove fn1

$(".content_media").unbind("touchstart").live("touchstart",fn2);

but if you just want fn1 then you don't have to use unbind actually...

edit

if for the reason why you're doing that is because you are calling this line,

$(".content_media").unbind("touchstart").live("touchstart",function(){....});

in every ajax execution... then that justifies it...
but you're doing it the wrong way....

just put it outside ajax execution and without unbind, then you're good to go...

$(".content_media").live("touchstart",function(){....});
海之角 2024-12-03 07:21:32

live()bind() 是实时事件冒泡到文档,然后由 jQuery 处理并检查触发的元素事件。因此,如果您在调用 live 之后添加更多与选择器匹配的元素,它们都会触发该事件。

然而,Bind() 是特定于元素的,您必须手动绑定所有新元素。从某种意义上说,如果您首先绑定一些元素并添加更多元素(即使与绑定选择器匹配),您将不得不再次绑定它们(但要小心不要双重绑定原始元素)。

Unbind()bind()die()live().您只能取消绑定使用 bind() 设置的事件,并且只能取消绑定使用 live() 设置的 die() 事件。另外值得一提的是,与通过绑定添加的事件不同,您无法阻止实时事件的传播。

The difference between live() and bind() is that live events bubble to the document and then they are handled by jQuery and checking what element fired the event. Therefore, if you add more elements that match the selector AFTER you have already called live, they will all fire the event.

Bind(), however is element specific and you have to manually bind all new elements. In the sense that if you bind some elements at first and add more (even if the match the bind selector) you're gonna have to bind them again (but be careful not to doublebind the original elements).

Unbind() is to bind(), what die() is to live(). You can only unbind events that are set with bind(), and you can only die() events that are set with live(). Also it's worth mentioning that you cannot stop live events from propagating, unlike events that were added with bind.

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