【问题标题】:How can I delete a jQuery event handler?如何删除 jQuery 事件处理程序?
【发布时间】:2011-12-30 21:45:24
【问题描述】:

我有这个:

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

testInstance = new test();

testInstance = null;  // delete window.testInstace;

虽然我已通过将testInstance 设置为null 来删除对对象的引用(我也尝试将其作为window 的属性删除),但mousemove 事件处理程序继续运行并写入控制台。如果删除建立事件处理程序的对象没有删除它,那么我应该怎么做才能删除事件处理程序?

【问题讨论】:

标签: javascript jquery object


【解决方案1】:

如果您使用的是 jquery 1.7

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

其他

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

【讨论】:

    【解决方案2】:

    销毁对象不会对您添加的事件处理程序产生任何影响。为了删除事件处理程序,您需要unbind 事件。

    【讨论】:

      猜你喜欢
      • 2012-09-04
      • 1970-01-01
      • 2010-11-21
      • 2010-10-22
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多