如何通过live()获取绑定到元素的事件信息?

How can I get information about events that are bound to an element via live()?

本文关键字:元素 事件 信息 绑定 live 何通过 获取      更新时间:2023-09-26

我有一个<a>标记,并将两个事件绑定到它。我如何获得与它相关的其他事件的信息?

下面的代码解释了我的意思:
<a href="#" id="sample" class="sample-cls">Click me</a>
$(function(){
    $('#sample').live("click", function(){
        sampleFunction();
    })
    $('.sample-cls').live("click", function(){
        // How to get information that to this link 
        // is attached another event, that run sampleFunction() ?
    })
})

遗憾的是,$('#sample').data('events')不包括通过live()绑定的事件。

// 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.toString() );
    });
});

看到这个

当然,这可以进一步增强以满足您的需要。