首页 > 【冷知识】获取网页所有的监听事件类型、方法。请认准getEventListeners

【冷知识】获取网页所有的监听事件类型、方法。请认准getEventListeners

 

 

  获取事件列表

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");

打印效果

更多相关:

  •   来源:https://www.cnblogs.com/hnsongbiao/p/9815808.html   偶然发现 C# 的 HttpRequest 要比 Chrome 请求同一Url 慢好多。C# HttpRequest 要500毫秒 而Chrome 只需要 39ms。   后来 整理 各种方法做了优化    HttpW...

  •   在后台获取upload file 数量的时候发现count一直为0,经检查发现了问题 ,代码如下:   前台: var data = $("#DetailForm").serialize(); $.ajax({ url: '@Url.Action("SaveRequest", "RegistrationRequest")', ty...

  •  * 启用ViewState的情况下,设置某一服务器控件的Value后,然后再将期Visible设置成false * 在回传时(PostBack)其Value不会丢失,ViewState会保留状态 * 如 if(!IsPostBack){ *    txtName.Text="xxxx"; *    txtName.Visible=f...

  •   测试用prototxt   name: "CIFAR10_quick"layer {name: "data" type: "MemoryData" top: "data" top: "label" memory_data_param {batch_size: 1     #样本个数 channels: 3 height: 32 w...

  •    话说每到吃完的时间就发愁,真的不知道该吃什么,然后就想到做一个生成吃什么的小软件,既然这个软件如此的简单,就打算用wpf开发吧,也不用数据库了,直接保存在xml中就可以了  程序整体结构如下图  首先我写了一个xml的帮助类,主要是写了个常用的增加方法 主程序界面也很简单,一共就两个页面 对应的两处代码也粘上 pr...