javascript jQuery:如何解除绑定到文档对象的事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16964008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: how to unbind an event binded to document object?
提问by realtebo
I've a bad-programmed library which is doing this
我有一个程序错误的库正在执行此操作
$(document).on('click','#keep_first_only_button', function(){
I wrote, after this, a piece of code to 'override' this bad behaviour
在此之后,我写了一段代码来“覆盖”这种不良行为
$("keep_first_only_button").unbind("click");
$("keep_first_only_button").on("click", selectKeepFirstOfAll);
BUT this is not working, then document.click function handler is triggered again
但是这不起作用,然后再次触发 document.click 函数处理程序
I cannot unbind all click events from document, because disasters will happen in the page.
我无法从文档中解除所有点击事件的绑定,因为页面中会发生灾难。
Which is the right way to proceed in this situation?
在这种情况下,哪种方法是正确的?
EDIT: Sorry for time loosing question, I didn't see the missing '#' in my selector. I'm really sorry !
编辑:抱歉浪费时间,我在选择器中没有看到缺少的“#”。我真的很抱歉 !
采纳答案by dKen
$("#keep_first_only_button")...
Missing the hash?
缺少哈希?
回答by Mattias Buelens
The original event handler was bound as a delegatedevent, so you can't remove it from $('#keep_first_only_button')
itself. You need to remove it on the document
level.
原始事件处理程序被绑定为委托事件,因此您无法将其从$('#keep_first_only_button')
自身中删除。您需要在document
关卡中将其删除。
From the documentation:
从文档:
To remove specific delegated event handlers, provide a
selector
argument. The selector string must exactly match the one passed to.on()
when the event handler was attached. To remove all delegated events from an element without removing non-delegated events, use the special value"**"
.
要删除特定的委托事件处理程序,请提供一个
selector
参数。选择器字符串必须与.on()
附加事件处理程序时传递的字符串完全匹配 。要从元素中删除所有委托事件而不删除非委托事件,请使用特殊值"**"
。
In other words, to unbind a delegated event, you should just use the same set of arguments you used to bind them but pass them to off
instead. Since the bound function is anonymous you can't reference it, so you'll have to settle with unbinding alldelegated event handlers bound to #keep_first_only_button
on the document
level:
换句话说,要解除委托事件的绑定,您应该只使用与绑定它们相同的一组参数,而是将它们传递给off
。由于绑定功能是匿名的,你不能引用它,所以你必须与解除绑定解决所有绑定委派的事件处理程序#keep_first_only_button
的document
级别:
$(document).off('click', '#keep_first_only_button');
EDIT:Looks like the problem was just the missing hash. Odd, I thought you couldn't unbind delegated event handlers using a regular.off()
call...
编辑:看起来问题只是缺少哈希。奇怪的是,我认为您无法使用常规.off()
调用取消绑定委托的事件处理程序...
回答by Trey Tyler
For anyone wondering ...Mattias Buelens is correct.
对于任何想知道的人……Mattias Buelens 是正确的。
If you have the element bound like
如果你有像这样绑定的元素
$(document).on("click","#element",function(){ });
And want to shut it off later, you have to do it as:
并且想稍后关闭它,您必须这样做:
$("#element").click(function(e) {
$(document).off('click', '#element');
});
回答by Damith Asanka
If you want remove event with out consider selector use
如果您想删除事件而不考虑选择器使用
$(document).off("click","**");
$(document).off("click","**");