How to list all registered events of a DOM node using Javascript?

房东的猫 提交于 2019-12-12 08:26:50

问题


I can add or remove an event handler for a DOM node. Is it possible to find out all the registered events handlers of a given DOM node? I am referring to straight Javascript meaning no frameworks or toolkits like jQuery, dojo, Prototype, GWT, etc. If the answer is no, any reason why? Security issues?


回答1:


I know this is an old question, but just in case, for chrome you can use getEventListeners

https://developers.google.com/chrome-developer-tools/docs/commandline-api#geteventlistenersobject

as mentioned here:

https://stackoverflow.com/a/17466308/538752




回答2:


DOM Level 3 specifies eventListenerList - however, I'm not aware of any DOM implementation which supports this - or any other reliable way to list the event listeners. It seems to have been an oversight to this point.




回答3:


This works for Chrome/Safari console:

getEventListeners(document.getElementByID('myElementId'));



回答4:


Visual Event can show you which events are registered, but it only works with DOM level 0 attached events; the W3C level 2 implementation as well as the Internet Explorer proprietary method are not supported and/or cannot be retrieved.




回答5:


If your interest is to discover some event, in order to disable it - I came here because of that - I recommend to use the Firebug extension, with Mozilla Firefox. Selecting the part of the document, you are interested in, look at the right panel, the Events tab: you will see all events, and can even disable them.




回答6:


Also, in Google Chrome, please select the element and notice the number, it will show you $0 or any other number.

Then in console, type this code and press enter.

getEventListeners($0)

and then you will see the result. Please see the image below for more elaboration.



来源:https://stackoverflow.com/questions/1475337/how-to-list-all-registered-events-of-a-dom-node-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!