jQuery关于解除绑定的问题
我可以将 jQuery unbind() 与 live() 一起使用吗
,例如
$(".content_media").unbind("touchstart").live("touchstart",function(){....});
如果可以,它到底是什么意思?基本上我想了解解除绑定意味着什么
我有一个页面,我在文档上绑定元素准备就绪...并且在其之间有 AJAX 调用,它重写了相同的元素...现在我希望它们的行为完全相似次..这就是我使用 live() 的原因
如果这里可能存在一些例外情况,其中 live() 绑定不起作用,请纠正我。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,您可以使用
unbind()
Doc< /a> 与live()
Doc如图所示。此代码:
取消绑定通过 jQuery
bind()
设置的任何touchstart
事件侦听器,然后创建touchstart
侦听器对于当前和任何未来节点,具有content_media
类。请注意,要停止使用
live()
设置侦听器,您需要使用die()
文档。此外,在 jQuery 外部设置的侦听器并不总是受到这些方法的影响。
Yes, you can use
unbind()
Doc withlive()
Doc as shown.This code:
Unbinds any
touchstart
event-listeners that were set via jQuerybind()
, then createstouchstart
listeners for the current, and any future nodes with thecontent_media
class.Note that to stop listeners being set with
live()
you need to usedie()
Doc.Also, listeners set outside jQuery are not always affected by these means.
取消绑定意味着从一组元素中删除事件处理程序。你可以只删除一些事件处理程序
或所有事件处理程序
你可以在解除绑定后绑定新的实时事件,live()的注意事项是这样的(摘自文档)
Unbind means removing event handlers from a set of elemnts. you can remove only some event handlers
or all event handlers
You can bind new live events after unbinding them, the attention with live() is this (taken from documentation)
取消绑定意味着删除之前附加到元素的事件处理程序:
但是您不能使用
unbind
删除使用live
添加的事件处理程序,因为live
的工作方式不同(它将事件处理程序附加到文档根目录)。这就是
.die()
的用途:To unbind means to remove the event handlers you previously attached to an element:
But you cannot use
unbind
to remove an event handler added withlive
becauselive
works differently (it attaches the event handler to the document root).That's what
.die()
is for:我不明白你在这里解除绑定的意义是什么..
好吧,如果你有函数
fn1
然后你有它,那么在某个时刻,你想使用
fn2
code> 没有fn1
之类的,那么你最好先调用 unbind...以删除
fn1
但如果你只想要
fn1
那么你就不需要不必使用解除绑定实际上...编辑
如果你这样做的原因是因为你
在每次ajax执行中调用这一行...那么这就证明了它的合理性...
但你做错了......
只需将它放在ajax执行之外并且不取消绑定,那么你就可以开始......
I don't get what's your point for unbind here..
well, if you have function
fn1
and then you have it as,then at a certain point, you want to use
fn2
withoutfn1
as like,then your good to call unbind first... to remove
fn1
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,
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...
live()
和bind()
是实时事件冒泡到文档,然后由 jQuery 处理并检查触发的元素事件。因此,如果您在调用 live 之后添加更多与选择器匹配的元素,它们都会触发该事件。然而,
Bind()
是特定于元素的,您必须手动绑定所有新元素。从某种意义上说,如果您首先绑定一些元素并添加更多元素(即使与绑定选择器匹配),您将不得不再次绑定它们(但要小心不要双重绑定原始元素)。Unbind()
是bind()
,die()
与live().您只能取消绑定使用
bind()
设置的事件,并且只能取消绑定使用live()
设置的die()
事件。另外值得一提的是,与通过绑定添加的事件不同,您无法阻止实时事件的传播。The difference between
live()
andbind()
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 tobind()
, whatdie()
is tolive()
. You can only unbind events that are set withbind()
, and you can onlydie()
events that are set withlive()
. Also it's worth mentioning that you cannot stop live events from propagating, unlike events that were added with bind.