开发者

Is it possible to obtain a list of events bound to an element in jQuery?

开发者 https://www.devze.com 2022-12-11 14:24 出处:网络
As the question said, i need the list of events bound to a specific element. I mean events like click, mouseover etc bound to that element at the loading of the dom.

As the question said, i need the list of events bound to a specific element.

I mean events like click, mouseover etc bound to that element at the loading of the dom.

(Stupid) example:

$("#element").click(function()
{
    //stuff
});
$("#element").开发者_运维技巧mouseover(function()
{
    //stuff
});
$("#element").focus(function()
{
    //stuff
});

Result:

click, mouseover, focus


Every event is added to an array.

This array can be accessed using the jQuery data method:

$("#element").data('events')

To log all events of one object to fireBug just type:

console.log ( $("#element").data('events') )

And you will get a list of all bound events.


Update:

For jQuery 1.8 and higher you have to look into the internal jQuery data object:

$("#element").each(function(){console.log($._data(this).events);});
// or
console.log($._data($("#element")[0]).events);


You can access it by element.data('events');. Example:

var events = element.data('events');
for (var type in events) {
    // `type` is "click", "mouseover", "change", etc.
    for (var handler in events[type]) {
        // `handler` is the associated function.
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消