简体   繁体   English

Javascript:删除所有事件侦听器

[英]Javascript: Remove all event listeners

What's the best way to remove all event listeners? 删除所有事件侦听器的最佳方法是什么? I've got something like this (using jQuery) but I'm not sure if this is the best way to go about it: 我有这样的东西(使用jQuery),但是我不确定这是否是最好的方法:

// Remove all the event listernes
$('*').off('blur click change dblclick each error focus focusin focusout keydown keypress keyup load mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave popstate resize scroll select submit unload');
$(window).off('scroll');
$("body").find("*").off();

这将删除主体中所有元素上的所有事件侦听器

Does .unbind() not do the trick? .unbind()不会解决问题吗?

Eg: 例如:

 $("#click_for_hello").on("click",function(){ alert("hello"); }) $("#click_for_hello").unbind(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="click_for_hello">Click me</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM