簡體   English   中英

jQuery取消范圍內聲明的事件

[英]jQuery unbind events declared in scope

我有一些對象會創建附加到document的事件 它們是常見事件,例如mousemove,mousedown等。

然后,我想使$(document).unbind('mousemove')及其確定,但它可能會使插件的最終用戶創建的某些事件崩潰或導致與某些外部代碼沖突

是否可以僅刪除在某個范圍內聲明的所有事件

喜歡:

$.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
}

還是有其他方法可以管理事件組?

您可以使用命名空間事件處理程序

防爆

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

然后

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

演示: 小提琴

將對函數的引用傳遞給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 );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM