私のプログラムでは、 body
Webページ内のすべてのイベントを委任します。どうすればそれを削除できますか?これが私のコードです、ありがとう。
jQuery(function ($) {
$("body").on("mouseenter.noteEvent", "*", function (event) {
//doSomeThing();
});
$("body").on("mouseleave.noteEvent", "*", function (event) {
//doSomeThing();
});
$("body").on("click.noteEvent", "*", function (event) {
//doSomeThing();
});
});
とイベントの削除機能、しかしそれは失敗しました、何か提案がありますか?
var removeEvent = function(){
jQuery("body").off(".noteEvent", "*"); // "*" or "**" ?
//jQuery("body").off(".noteEvent", "**");
};
回答:
回答№1は0委任されたすべてのイベントハンドラーを名前空間でバインド解除する場合は、次のことを行うだけで済みます。
jQuery("body").off(".noteEvent"); // only specify the namespace.
回答№2の場合は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サイト.