Using jQuery, is it possible to get a list of all the events and to which element the event is bound to?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
- jQuery add and remove delay
you can check this tool http://www.sprymedia.co.uk/article/Visual+Event+2
I bet you could traverse the DOM and inspect the event attribute on each element building up a list... but I've never tried it.
jQuery makes this relatively easy because it stores the event handlers in the element data. You should be able to use something like this:
and you can call it:
UPDATE: I added a count of handlers and some information about delegated events to the output of the above function.
UPDATE (8/24/2012): While the function above still works in jQuery 1.7.2 and lower, jQuery no longer stores the event object in
jQuery.data(elem, 'events')
and if you are using jQuery 1.8 or later you will no longer be able to use the function above!In exchange for
jQuery.data(elem, 'events')
you can now usejQuery._data(elem, 'events')
. An update to the function above would look like this:UPDATE (4/25/2013):
andSelf()
is deprecated from 1.8.x http://bugs.jquery.com/ticket/9800 , I replaced withaddBack()
instead.To use console.table() I did some modifications...
So now I can do things like this:
See what happens at chrome:
I use this one to list all elements that has a bounded event.
It is also possible to collect elements as a list for each event by writing some additional codes like this: