【问题标题】:using .off function for multiple event handlers on a single selector对单个选择器上的多个事件处理程序使用 .off 函数
【发布时间】:2014-07-02 14:01:05
【问题描述】:

我有一个代码,其中我使用 jquery 的 .on 函数将多个事件处理程序附加到单个选择器

$(document).on({

     mouseenter: function() {
            console.log("handle enter");

        },
        mouseleave: function() {
            console.log("handle leave");

        }, 
        click: function(){
        console.log("clicked");
        }
},'.handle');

现在我想删除所有这些事件处理程序。我知道这可以通过以下方式使用 .off 来实现

$(document).off("mouseenter",".handle");
$(document).off("mouseleave",".handle");
$(document).off("click",".handle");

但我担心的是我只想使用 .off 函数一次并实现上述目的。 我怎样才能做到这一点? 任何帮助将不胜感激。

【问题讨论】:

    标签: javascript jquery event-handling live jquery-on


    【解决方案1】:

    您可以只用空格分隔事件,请参阅此处 .off 函数的第二个描述:http://api.jquery.com/off

    $(document).off("mouseenter mouseleave click",".handle");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      • 2012-01-26
      相关资源
      最近更新 更多