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

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

问题描述

借助 jQuery,您可以使用 .bind() 或事件处理程序辅助函数之一将函数绑定到在 DOM 对象上触发的事件.

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 必须以某种方式在内部存储它,我想知道是否有可能给定一个 DOM 对象,找出哪些事件已绑定到该对象,并访问这些函数等.所需的返回结果可能如下所示:

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]
}

推荐答案

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

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

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

似乎 jQuery 使用 data 来存储事件处理程序:

It seems that jQuery uses data to store event handlers:

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

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天全站免登陆