Javascript:删除所有事件监听器

时间:2018-01-28 17:00:18

标签: javascript jquery

删除所有事件监听器的最佳方法是什么?我有这样的东西(使用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');

2 个答案:

答案 0 :(得分:1)

$("body").find("*").off();

这将删除正文中所有元素的所有事件侦听器

答案 1 :(得分:0)

.unbind()没有做到这一点吗?

E.g:

$("#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>

相关问题