事件处理jQuery unclick()和unbind()事件?

时间:2021-04-24 19:47:01

I want to attach a click event to a button element and then later remove it, but I can't get unclick() or unbind() event(s) to work as expected. In the code below, the button is 'tan' colour and the click event works.

我想将一个click事件附加到一个按钮元素,然后将其删除,但我无法使unlick()或unbind()事件按预期工作。在下面的代码中,按钮为“棕褐色”,点击事件有效。

window.onload = init; 
function init() {
    $("#startButton").css('background-color', 'beige').click(process_click);
    $("#startButton").css('background-color', 'tan').unclick();
}

How can I remove events from my elements?

如何从元素中删除事件?

4 个解决方案

#1


19  

There's no such thing as unclick(). Where did you get that from?

没有unlick()这样的东西。你从哪里得到那个的?

You can remove individual event handlers from an element by calling unbind:

您可以通过调用unbind从元素中删除单个事件处理程序:

$("#startButton").unbind("click", process_click);

If you want to remove all handlers, or you used an anonymous function as a handler, you can omit the second argument to unbind():

如果要删除所有处理程序,或者使用匿名函数作为处理程序,则可以省略unbind()的第二个参数:

$("#startButton").unbind("click");

#2


6  

Or you could have a situation where you want to unbind the click function just after you use it, like I had to:

或者你可能会在你使用它之后想要取消绑定点击功能的情况,就像我必须:

$('#selector').click(function(event){
    alert(1);
    $(this).unbind(event);
});

#3


3  

unbind is your friend.

unbind是你的朋友。

$("#startButton").unbind('click')

#4


0  

Are you sure you want to unbind it? What if later on you want to bind it again, and again, and again? I don't like dynamic event-handling bind/unbind, since they tend to get out of hand, when called from different points of your code.

你确定要解开吗?如果你以后想要再次绑定它又怎么样呢?我不喜欢动态事件处理bind / unbind,因为当从代码的不同点调用时,它们往往会失控。

You may want to consider alternate options:

您可能需要考虑其他选项:

  • change the button "disabled" property
  • 更改按钮“已禁用”属性

  • implement your logic inside "process_click" function
  • 在“process_click”函数中实现你的逻辑

Just my 2 cents, not an universal solution.

只需2美分,不是通用的解决方案。

#1


19  

There's no such thing as unclick(). Where did you get that from?

没有unlick()这样的东西。你从哪里得到那个的?

You can remove individual event handlers from an element by calling unbind:

您可以通过调用unbind从元素中删除单个事件处理程序:

$("#startButton").unbind("click", process_click);

If you want to remove all handlers, or you used an anonymous function as a handler, you can omit the second argument to unbind():

如果要删除所有处理程序,或者使用匿名函数作为处理程序,则可以省略unbind()的第二个参数:

$("#startButton").unbind("click");

#2


6  

Or you could have a situation where you want to unbind the click function just after you use it, like I had to:

或者你可能会在你使用它之后想要取消绑定点击功能的情况,就像我必须:

$('#selector').click(function(event){
    alert(1);
    $(this).unbind(event);
});

#3


3  

unbind is your friend.

unbind是你的朋友。

$("#startButton").unbind('click')

#4


0  

Are you sure you want to unbind it? What if later on you want to bind it again, and again, and again? I don't like dynamic event-handling bind/unbind, since they tend to get out of hand, when called from different points of your code.

你确定要解开吗?如果你以后想要再次绑定它又怎么样呢?我不喜欢动态事件处理bind / unbind,因为当从代码的不同点调用时,它们往往会失控。

You may want to consider alternate options:

您可能需要考虑其他选项:

  • change the button "disabled" property
  • 更改按钮“已禁用”属性

  • implement your logic inside "process_click" function
  • 在“process_click”函数中实现你的逻辑

Just my 2 cents, not an universal solution.

只需2美分,不是通用的解决方案。