我可以使用 jQuery 找到绑定在元素上的事件吗? [英] Can I find events bound on an element with jQuery?

查看:29
本文介绍了我可以使用 jQuery 找到绑定在元素上的事件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此链接上绑定了两个事件处理程序:

I bind two event handlers on this link:

<a href='#' id='elm'>Show Alert</a>

JavaScript:

$(function()
{
  $('#elm').click(_f);
  $('#elm').mouseover(_m);
});

function _f(){alert('clicked');}
function _m(){alert('mouse over');}

有没有办法获取绑定在一个元素上的所有事件的列表,在这种情况下是在带有 id="elm" 的元素上?

Is there any way to get a list of all events bound on an element, in this case on element with id="elm"?

推荐答案

在现代版本的 jQuery 中,您将使用 $._data 方法来查找由 jQuery 附加到相关元素的任何事件.注意,这是一个仅供内部使用的方法:

In modern versions of jQuery, you would use the $._data method to find any events attached by jQuery to the element in question. Note, this is an internal-use only method:

// Bind up a couple of event handlers
$("#foo").on({
    click: function(){ alert("Hello") },
    mouseout: function(){ alert("World") }
});

// Lookup events for this particular Element
$._data( $("#foo")[0], "events" );

来自 $._data 的结果将是一个包含我们设置的两个事件的对象(下图为 mouseout 属性展开):

The result from $._data will be an object that contains both of the events we set (pictured below with the mouseout property expanded):

然后在 Chrome 中,您可以右键单击处理程序函数并单击查看函数定义"以显示它在代码中定义的确切位置.

Then in Chrome, you may right click the handler function and click "view function definition" to show you the exact spot where it is defined in your code.

这篇关于我可以使用 jQuery 找到绑定在元素上的事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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