2014-03-27 25 views
0

在我的程序中,我使用body委託在網頁中的所有事件。我怎樣才能刪除它?這是我的代碼,謝謝。jQuery:如何刪除所有委託事件

jQuery(function ($) { 

    $("body").on("mouseenter.noteEvent", "*", function (event) { 
     //doSomeThing(); 
    }); 

    $("body").on("mouseleave.noteEvent", "*", function (event) { 
     //doSomeThing(); 
    }); 

$("body").on("click.noteEvent", "*", function (event) { 
     //doSomeThing(); 
    }); 

}); 

和remove event函數,但是失敗了,有什麼建議嗎?

var removeEvent = function(){ 

    jQuery("body").off(".noteEvent", "*"); // '*' or '**' ? 
    //jQuery("body").off(".noteEvent", "**"); 
}; 

回答

0

您可以使用下面的刪除事件:

$("body").off("mouseenter.noteEvent"); 

或者,如果你使用:

$("body").off("mouseenter"); 

然後所有的mouseenter事件將被刪除。您也可以使用:

// Remove all event handlers from all paragraphs: 
$("p").off(); 

// Remove all delegated mouseenter handlers from all paragraphs: 
$("p").off("mouseenter", "**"); 

// Remove event handlers in the ".noteEvent" namespace 
$("body").off(".noteEvent"); 

瞭解更多關於jQuery Site

0

當他們的命名空間取消綁定所有委派的事件處理程序,你只需要做到:

jQuery("body").off(".noteEvent"); // only specify the namespace.