jQuery:如何解除绑定到文档对象的事件?

时间:2021-06-16 18:53:55

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 !

编辑:抱歉没时间丢失问题,我没有在选择器中看到丢失的'#'。我真的很抱歉 !

4 个解决方案

#1


7  

$("#keep_first_only_button")...

Missing the hash?

缺少哈希?

#2


25  

The original event handler was bound as a delegated event, 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')本身中删除。您需要在文档级别删除它。

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 "**".

要删除特定的委托事件处理程序,请提供选择器参数。选择器字符串必须与附加事件处理程序时传递给.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 all delegated event handlers bound to #keep_first_only_button on the document level:

换句话说,要取消绑定委托事件,您应该使用您用来绑定它们的相同参数集,但是将它们传递给off。由于绑定函数是匿名的,因此无法引用它,因此您必须解决绑定到文档级别的#keep_first_only_button的所有委托事件处理程序:

$(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()调用解除委托事件处理程序的绑定...

#3


5  

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'); 
});

#4


0  

If you want remove event with out consider selector use

如果你想删除事件而不考虑选择器使用

$(document).off("click","**");

#1


7  

$("#keep_first_only_button")...

Missing the hash?

缺少哈希?

#2


25  

The original event handler was bound as a delegated event, 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')本身中删除。您需要在文档级别删除它。

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 "**".

要删除特定的委托事件处理程序,请提供选择器参数。选择器字符串必须与附加事件处理程序时传递给.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 all delegated event handlers bound to #keep_first_only_button on the document level:

换句话说,要取消绑定委托事件,您应该使用您用来绑定它们的相同参数集,但是将它们传递给off。由于绑定函数是匿名的,因此无法引用它,因此您必须解决绑定到文档级别的#keep_first_only_button的所有委托事件处理程序:

$(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()调用解除委托事件处理程序的绑定...

#3


5  

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'); 
});

#4


0  

If you want remove event with out consider selector use

如果你想删除事件而不考虑选择器使用

$(document).off("click","**");