获取事件列表
getEventListeners(window)//获取window绑定的所有监听事件列表
//----------------------------------------
getEventListeners(document.querySelector("选择器"))//获取指定DOM的所有监听事件列表
这里以CSDN为例,F12当前页面在Console选项卡输入 getEventListeners(window),回车执行代码后会输出以下对象:
{
"load": [
{"useCapture": false, "passive": false, "once": false, "type": "load"},
{"useCapture": false, "passive": false, "once": false, "type": "load"},
{"useCapture": false, "passive": false, "once": false, "type": "load"},
{"useCapture": false, "passive": false, "once": false, "type": "load"},
{"useCapture": false, "passive": false, "once": false, "type": "load"}
],
"unload": [
{"useCapture": false, "passive": false, "once": false, "type": "unload"},
{"useCapture": false, "passive": false, "once": false, "type": "unload"},
{"useCapture": false, "passive": false, "once": false, "type": "unload"},
{"useCapture": false, "passive": false, "once": false, "type": "unload"},
{"useCapture": false, "passive": false, "once": false, "type": "unload"}
],
"beforeunload": [
{"useCapture": false, "passive": false, "once": false, "type": "beforeunload"}
],
"pagehide": [
{"useCapture": false, "passive": false, "once": false, "type": "pagehide"},
{"useCapture": false, "passive": false, "once": false, "type": "pagehide"}
],
"error": [
{"useCapture": false, "passive": false, "once": false, "type": "error"}
],
"keydown": [
{"useCapture": false, "passive": false, "once": false, "type": "keydown"}
],
"resize": [
{"useCapture": false, "passive": false, "once": false, "type": "resize"},
{"useCapture": false, "passive": false, "once": false, "type": "resize"},
{"useCapture": false, "passive": false, "once": false, "type": "resize"},
{"useCapture": false, "passive": false, "once": false, "type": "resize"}
],
"scroll": [
{"useCapture": false, "passive": false, "once": false, "type": "scroll"},
{"useCapture": false, "passive": false, "once": false, "type": "scroll"},
{"useCapture": false, "passive": false, "once": false, "type": "scroll"},
{"useCapture": false, "passive": false, "once": false, "type": "scroll"}
],
"orientationchange": [
{"useCapture": false, "passive": false, "once": false, "type": "orientationchange"}
],
"pageshow": [
{"useCapture": false, "passive": false, "once": false, "type": "pageshow"}
],
"focus": [
{"useCapture": false, "passive": false, "once": false, "type": "focus"}
],
"blur": [
{"useCapture": false, "passive": false, "once": false, "type": "blur"}
],
"DOMContentLoaded": [
{"useCapture": false, "passive": false, "once": false, "type": "DOMContentLoaded"},
{"useCapture": false, "passive": false, "once": false, "type": "DOMContentLoaded"},
{"useCapture": false, "passive": false, "once": false, "type": "DOMContentLoaded"}
],
"keyup": [
{"useCapture": false, "passive": false, "once": false, "type": "keyup"}]
};
获取事件绑定的方法源码
getEventListeners(window).事件名[0].listener;
//----------------------------------------
getEventListeners(document.querySelector("选择器")).事件名[0].listener;
触发事件绑定的方法
getEventListeners(window).事件名[0].listener();
//----------------------------------------
getEventListeners(document.querySelector("选择器")).事件名[0].listener();
移除对应绑定的事件监听(尤其是某些页面禁止了复制、切屏的时候屡试不爽,你懂得[坏笑])
var event=getEventListeners(window).事件名[0];
window.removeEventListener(event.type,event.listener);
//----------------------------------------
var dom=document.querySelector("选择器");
var event=getEventListeners(dom).事件名[0];
dom.removeEventListener(event.type,event.listener);
批量移除所有监听事件(硬核!)
var events=getEventListeners(window)
for (var eventName in events) {var eventItems = events[eventName];
console.log(event);
for (var i = 0, l = eventItems.length; i < l; i++) {
var eventItem = eventItems[i];
window.removeEventListener(eventItem.type,eventItem.listener);
}
}
//----------------------------------------
var dom=document.querySelector("选择器")
var events=getEventListeners(dom)
for (var eventName in events) {var eventItems = events[eventName];
console.log(event);
for (var i = 0, l = eventItems.length; i < l; i++) {
var eventItem = eventItems[i];
dom.removeEventListener(eventItem.type,eventItem.listener);
}
}
批量移除并打印指定DOM及其子元素所有监听方法代码(实用!)
var getAllEvents = function (sel) {
var doms = [document.querySelector(sel)].concat([].slice.call(document.querySelectorAll(sel + " *")));//包括元素自己
for (var i = 0, l = doms.length; i < l; i++) {
var dom = doms[i];
var events = getEventListeners(dom);
for (var eventName in events) {
var eventItems = events[eventName];
for (var j = 0, len = eventItems.length; j < len; j++) {
var eventItem = eventItems[j];
dom.removeEventListener(eventItem.type, eventItem.listener);//移除事件
console.log(`事件[${eventItem.type}]监听方法`, eventItem.listener);
}
}
}
};
getAllEvents(".className");
打印效果