Accessing functions bound to event handlers with jQuery

浪尽此生 提交于 2019-11-26 21:43:00
PatrikAkerstrand

Edit: the method below works only in jQuery < 1.7

You can find a lot of interesting tips and tricks in this article: Things you may not know about jQuery.

It seems that jQuery uses data to store event handlers:

You can access all event handlers bound to an element (or any object) through jQuery’s event storage:

// List bound events:
console.dir( jQuery('#elem').data('events') );

// Log ALL handlers for ALL events:
jQuery.each($('#elem').data('events'), function(i, event){
    jQuery.each(event, function(i, handler){
        console.log( handler['handler'].toString() );
    });
});

// You can see the actual functions which will occur
// on certain events; great for debugging!

jQuery 1.7 has stopped exposing the events in the regular data() function. You can still get them like this:

var elem = $('#someid')[0];
var data = jQuery.hasData( elem ) && jQuery._data( elem );
console.log(data.events);

Please note, that this only works for Events which have been bound using jQuery. AFAIK you there is no way to see all the events which have been bound using the regular DOM functions like addEventListener.

You can see them in the webkit inspector though: In the Elements tab navigate to the desired DOM node, on the right side select the "Event Listeners" drop down.

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