0

I've got some object that creates some event attached to document. They are common events like mousemove, mousedown etc.

Then I want to make $(document).unbind('mousemove') and its ok but it may crash some events created by end user of plugin or cause conflicts with some external code.

Is it possible to remove all events declared inside some scope only?

Like:

$.fn.somePlugin = function() {
    //here begins our scope

    //declaring a lot of events

    //some stuff

    //remove events of this scope leaving other of same type untouched
}

Or is there any other way of managing groups of events?

4

2 回答 2

3

您可以使用命名空间的事件处理程序

前任

$(el).on('click.myplugin', function(){...})//or any eventname.pluginname

然后

$(el).off('click.myplugin')

演示:小提琴

于 2013-09-30T16:00:30.983 回答
0

将函数的引用传递给off方法。

来自http://api.jquery.com/off/

var foo = function() {
  // Code to handle some kind of event
};

// ... Now foo will be called when paragraphs are clicked ...
$( "body" ).on( "click", "p", foo );

// ... Foo will no longer be called.
$( "body" ).off( "click", "p", foo );
于 2013-09-30T16:03:22.800 回答