使用 jQuery 访问绑定到事件处理程序的函数
通过 jQuery,您可以使用 .bind()
或事件处理程序辅助函数之一将函数绑定到 DOM 对象上触发的事件。
jQuery 必须以某种方式在内部存储它,我想知道是否有可能给定一个 DOM 对象,找出哪些事件已绑定到该对象,并访问这些函数等。所需的返回结果可能如下所示:
{
click: [function1, function2],
change: [function3],
blur: [function4, function5, function6]
}
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]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jQuery 1.7 已停止公开常规 data() 函数中的事件。您仍然可以这样获取它们:
请注意,这仅适用于使用 jQuery 绑定的事件。据我所知,您无法查看使用常规 DOM 函数(如 addEventListener)绑定的所有事件。
不过,您可以在 webkit 检查器中看到它们:在 Elements 选项卡中导航到所需的 DOM 节点,在右侧选择“Event Listeners”下拉列表。
jQuery 1.7 has stopped exposing the events in the regular data() function. You can still get them like this:
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.
编辑:下面的方法仅适用于 jQuery 1.7
您可以在本文中找到很多有趣的提示和技巧:关于 jQuery 你可能不知道的事情。
jQuery 似乎使用
data
来存储事件处理程序: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: