javascript jquery .off 似乎不起作用

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

jquery .off doesn't seem to work

javascriptjquery

提问by MirrorMirror

so I'll be short: jquery .off()doesn't disable a listen I've set with .on.

所以我会很简短:jquery.off()不会禁用我设置的监听.on

html:

html:

<span id="myspan">lol</span>
<button id="b1">jquery On</button>
<button id="b2">jquery Off</button>

js:

js:

$("#b1").on("click", add);
$("#b2").on("click", del);

function add() {
    $("#myspan").on("click", function(e) {
        var a = 1;
        testfunc(a, e);
    });
}

function del() {
    $("#myspan").off("click", testfunc);
}

function testfunc(num, event) {
   alert(num);
}

So first we add to myspanthe testfunc()by clicking the jquery Onbutton. After we do that, if we click on the span, we get an alert. Next, we click the jquery offbutton. That is supposed to remove the listener but it doesn't. Even after that, when we click on myspantestfuncis still attached.

所以首先我们添加到myspantestfunc()点击jQuery的关于按钮。在我们这样做之后,如果我们点击跨度,我们会收到警报。接下来,我们单击jquery off按钮。这应该删除侦听器,但事实并非如此。即使在那之后,当我们点击myspan 时testfunc仍然附加。

Why? And how can I remove it ?

为什么?我该如何删除它?

回答by Robert Koritnik

Your parameters don't match

您的参数不匹配

It doesn't because you bound to a different function (anonymous one). And then you're trying to unbind from testfunc... In order for your event (un)binding to work both parameters between onand offmust match.

这不是因为您绑定到不同的功能(匿名功能)。然后您试图从testfunc...解除绑定,以便您的事件 (un)binding 能够onoffmust match之间使用两个参数

Possible workaround

可能的解决方法

If this is the only clickevent listener on the element, then it's be easiest way to unbind from your anonymous function by calling:

如果这是click元素上唯一的事件侦听器,那么通过调用以下方法解除匿名函数的绑定是最简单的方法:

$("#myspan").off("click");

If you're binding several event handlers to click event on the same element then you can also distinguish them by providing namespaces and then use proper namespacing in offcall.

如果您将多个事件处理程序绑定到同一元素上的 click 事件,那么您还可以通过提供命名空间来区分它们,然后在off调用中使用正确的命名空间。

$("#myspan").on("click.test", function(e) { ... });
...
$("#myspan").off("click.test");

Or use just namespace if you'd like to unbind several different event handlers that were bound using the same namespace:

或者,如果您想取消绑定使用相同命名空间绑定的多个不同事件处理程序,则仅使用命名空间:

$("#myspan").off(".test");

回答by adeneo

You're not binding the event handler to testfunc, you're binding it to an anonymous function, and whitin that function you're just calling testfunc, so you can't automatically unbind that.

您没有将事件处理程序绑定到testfunc,而是将其绑定到一个匿名函数,并在您刚刚调用的那个函数中添加了testfunc,因此您无法自动解除绑定。

It's either

要么

$("#myspan").on("click", testfunc); // bind function

and then

接着

$("#myspan").off("click", testfunc); // unbind function

or to unbind the anonymous function, just

或者解除匿名函数的绑定,只需

$("#myspan").off("click"); // remove all handlers

or you can also namespace the handler

或者你也可以命名处理程序

$("#myspan").on("click.test", function(e) {
    var a = 1;
    testfunc(a, e);
});

and to remove only that handler

并仅删除该处理程序

$("#myspan").off("click.test");

回答by Jens Neubauer

In this simple case, replace your offcall with this:

在这个简单的例子中,off用这个替换你的电话:

function del() {
    $("#myspan").off("click");
}

You don't need to pass the handler function to the offcall, and if you do, it will only remove that particular handler. However, you did not attach testfunc, but an anonymous function that just calls testfunc(). Therefore, your offcall does nothing.

您不需要将处理程序函数传递给off调用,如果这样做,它只会删除该特定处理程序。但是,您没有附加testfunc,而是一个仅调用testfunc(). 因此,您的off调用没有任何作用。

Also, you never assigned the variable testfunc.

此外,您从未分配过变量testfunc