【问题标题】:jquery unbind special event?jquery取消绑定特殊事件?
【发布时间】:2012-06-11 16:23:25
【问题描述】:

有我的代码,我想在 mousemove 时提醒“测试”一次,然后删除 mousemove 事件,但我使用 unbind() 似乎不起作用,有人可以帮助我吗??

$(document).ready( function(){
  $(document).mousemove( function(){
    alert( "test" );
    $(document).unbind("mousemove");
  });
});

【问题讨论】:

  • works for me。但是你可以使用one 来缩短你的代码。
  • thansk @F.Calderan @James Allardice 寻求帮助,我正在尝试使用.one(),现在可以使用,但.unbind() 仍然无法使用!!

标签: javascript jquery jquery-selectors


【解决方案1】:

只需使用$.one() 绑定一次性使用事件处理程序。

$(document).ready(function() {
    $(document).one("mousemove", function() {
        alert("test");
    });
});

【讨论】:

  • 谢谢Mattias Buelens这个方法我用过,现在可以了,谢谢你的帮助!!
【解决方案2】:

我认为它现在 onoff 而不是 bind/unbind

你可以试试

$(document).off("mousemove");

【讨论】:

  • 虽然onoff 现在确实是首选方法,但unbind 仍然有效,因此这不太可能产生影响。
  • 实际上 unbind 只会调用 off 方法,所以最好使用 off() 来作为最佳实践
  • 我正在尝试使用.off(),但它不能工作,然后我试试这个.one(),它工作。感谢每一位帮助我的人,我很感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
  • 2010-12-20
  • 1970-01-01
  • 1970-01-01
  • 2010-10-22
  • 1970-01-01
相关资源
最近更新 更多