2011-12-30 170 views
3

我有这样的:如何删除jQuery事件处理程序?

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

testInstance = new test(); 

testInstance = null; // delete window.testInstace; 

虽然我已经通过设置testInstance空(我也尝试删除它作为window属性)拆下来的对象引用,鼠标移动事件处理程序继续运行并写到控制台。如果删除建立事件处理程序的对象不会删除它,那么我应该如何删除事件处理程序?

+1

您必须显式解除绑定事件处理程序。 – 2011-12-30 21:47:51

+1

结帐http://stackoverflow.com/questions/742623/deleting-objects-in-javascript – Yaniro 2011-12-30 21:48:22

+0

JS没有“删除”对象的概念。 – Kos 2011-12-30 21:54:22

回答

13

,如果您使用jQuery 1.7

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

其他

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

销毁对象不会对你已经添加的事件处理程序没有任何影响。为了移除事件处理程序,您需要unbind事件。