简体   繁体   English

如何删除jQuery事件处理程序?

[英]How can I delete a jQuery event handler?

I have this: 我有这个:

function test()
{
    this.method = function ()
                {
                    $("html").mousemove(function(event) {
                        console.log('~> moved');
                    });
                }
    this.method();
}

testInstance = new test();

testInstance = null;  // delete window.testInstace;

Although I have removed references to the object by setting testInstance to null (I've also tried deleting it as a property of window ), the mousemove event handler continues to operate and write to the console. 虽然我已经通过将testInstance设置为null来删除对该对象的引用(我还尝试将其作为window的属性删除),但mousemove事件处理程序继续操作并写入控制台。 If deleting the object that established the event handler doesn't remove it what then should I do to remove the event handler? 如果删除建立事件处理程序的对象没有删除它,那么我该怎么做才能删除事件处理程序?

if you are using jquery 1.7 如果您使用的是jquery 1.7

$('html').off('mousemove');

else 其他

$('html').unbind('mousemove');

Destroying the object will not have any effect on the event handler that you've added. 销毁对象不会对您添加的事件处理程序产生任何影响。 In order to remove the event handler, you need to unbind the event. 要删除事件处理程序,您需要取消绑定事件。

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

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