简体   繁体   English

jquery .off似乎不起作用

[英]jquery .off doesn't seem to work

so I'll be short: jquery .off() doesn't disable a listen I've set with .on . 所以我会很简短:jquery .off()不会禁用我用.on设置的.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 myspan the testfunc() by clicking the jquery On button. 首先,我们通过单击jquery On按钮将testfunc()添加到myspan After we do that, if we click on the span, we get an alert. 在我们这样做之后,如果我们点击跨度,我们会收到警报。 Next, we click the jquery off button. 接下来,我们单击jquery off按钮。 That is supposed to remove the listener but it doesn't. 这应该是删除监听器,但它没有。 Even after that, when we click on myspan testfunc is still attached. 即使在那之后,当我们点击myspan testfunc仍然附加。

Why? 为什么? And how can I remove it ? 我该如何删除它?

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 on and off must match . 然后你试图从testfunc解除绑定...为了使你的事件(un)绑定工作onoff之间的参数必须匹配

Possible workaround 可能的解决方法

If this is the only click event 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 off call. 如果您将多个事件处理程序绑定到同一元素上的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: 如果您想取消绑定使用相同命名空间绑定的几个不同的事件处理程序,请使用just namespace:

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

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");

In this simple case, replace your off call with this: 在这个简单的情况下,更换您off这个调用:

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

You don't need to pass the handler function to the off call, and if you do, it will only remove that particular handler. 您不需要将处理函数传递给off函数,如果这样做,它将只删除该特定处理程序。 However, you did not attach testfunc , but an anonymous function that just calls testfunc() . 但是,您没有附加testfunc ,而是一个只调用testfunc()的匿名函数。 Therefore, your off call does nothing. 因此,您的off呼叫无效。

Also, you never assigned the variable testfunc . 此外,您从未分配变量testfunc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM