使用jQuery访问绑定到事件处理程序的函数 [英] Accessing functions bound to event handlers with jQuery

查看:162
本文介绍了使用jQuery访问绑定到事件处理程序的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jQuery可以使用 .bind()或其中一个事件处理程序助手函数将函数绑定到DOM对象上触发的事件。



jQuery必须以内部方式存储这个内容,我想知道是否可能给出一个DOM对象,找出哪些事件被绑定到对象,并访问这些函数等。所需的返回结果可以看起来像这样:

  {
点击:[function1,function2],
更改:功能3],
blur:[function4,function5,function6]
}


解决方案

编辑以下方法仅适用于jQuery< 1.7



您可以在本文中找到很多有趣的提示和技巧:你可能不知道的关于jQuery的事情



它似乎jQuery使用 数据 存储事件处理程序:


您可以访问所有事件处理程序
绑定到元素(或任何对象)
通过jQuery的事件存储:




  //列出绑定事件:
console.dir(jQuery ('#elem')。data('events'));

//记录所有事件的所有处理程序:
jQuery.each($('#elem')。data('events'),function(i,event){
jQuery.each(event,function(i,handler){
console.log(handler ['handler']。toString());
});
});

//您可以看到在某些事件上将发生
//的实际函数;伟大的调试!


With jQuery you can bind functions to an event triggered on a DOM object using .bind() or one of the event handler helper functions.

jQuery have to store this internally somehow and I wonder if is it possible given a DOM object, to find out which events have been bound to the object, and access those functions etc. The desired return result could look something like this:

{
  click: [function1, function2],
  change: [function3],
  blur: [function4, function5, function6]
}

解决方案

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访问绑定到事件处理程序的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆