jQuery jquery取消绑定点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/785147/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:47:10  来源:igfitidea点击:

jquery unbind click

jquery

提问by antonioh

I'm going nuts just trying to unbind an onclick handler from the event in jQuery so I can bind it later on to another function.

我只是想从 jQuery 中的事件中解除 onclick 处理程序的绑定,这样我就可以稍后将它绑定到另一个函数。

I have isolated the code in a test page so there's nothing but the meat, just a button that calls a function and a script that triesto unbind it:

我已经在测试页面中隔离了代码,所以除了肉之外什么都没有,只有一个调用函数的按钮和一个尝试解除绑定的脚本:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <script src="../../Scripts/jquery-1.3.2.min.js" type="text/javascript">
    </script>

    <script src="../../Scripts/jquery-1.3.2.js" type="text/javascript">
    </script>

    <script type="text/javascript" language="javascript">
        $(document).ready(function() { $("#btnNext").unbind('click'); });
        function hi() {window.alert("hi");}
    </script>
</head>

<body>

    <input id="btnNext" type="button" value="Next" onclick="hi();" />
</body>
</html>

Anybody knows why the click event keeps calling the hi() function no matter what I said in the document.ready?

有谁知道为什么不管我在 document.ready 中说什么,click 事件总是不断调用 hi() 函数?

Thanks

谢谢

回答by Chad Grant

Because you put it in the html attribute, it stays there. It was not bound with jQuery so jQuery is not tracking it's usage.

因为你把它放在 html 属性中,所以它留在那里。它没有与 jQuery 绑定,因此 jQuery 不会跟踪它的使用情况。

$("a").bind('click',hi);
$("a").unbind('click',hi);

http://docs.jquery.com/Events/bind

http://docs.jquery.com/Events/bind

回答by John Bledsoe

Just for the record, you can use the removeAttrjQuery function to remove the initially specified onclickattribute.

只是为了记录,您可以使用removeAttrjQuery 函数删除最初指定的onclick属性。

$('#btnNext').removeAttr("onclick");

Also, in response to Chad, I agree that it is generally better to do all your binding with jQuery, but I'm in a situation (using ASP.NET MVC) that requires individual links to have some model parameters in their click event handlers. It would be more convoluted IMO to try to wire up these event handlers via jQuery.

此外,作为对 Chad 的回应,我同意使用 jQuery 进行所有绑定通常会更好,但我处于一种情况(使用 ASP.NET MVC)需要单个链接在其单击事件处理程序中具有一些模型参数. IMO 尝试通过 jQuery 连接这些事件处理程序会更加复杂。

回答by spthorn

I couldn't get removeAttr to work, but this does:

我无法让 removeAttr 工作,但这样做:

$("#btnNext").click(function(){ return false; });

I was wanting to remove the ASP.NET-supplied onclicks (just for SelectedNodeChanged) in my TreeView, so I could handle the event on the client. Of course, my click function included more than "return false".

我想在我的 TreeView 中删除 ASP.NET 提供的 onclicks(仅用于 SelectedNodeChanged),以便我可以处理客户端上的事件。当然,我的点击功能不仅仅包括“返回假”。

回答by tsveti_iko

Let's say you have

假设你有

$(elem).on("click", function() { ... }); 

you don't even need to bind it if you want to unbind it later. To do so can just do:

如果您以后想取消绑定,您甚至不需要绑定它。这样做可以只做:

$(elem).unbind();

and that will remove the handlers regardless of type. If you want to be more specific, you can always pass an event type like "click".

无论类型如何,这都会删除处理程序。如果你想更具体,你总是可以传递一个事件类型,比如“点击”。