我可以使用jQuery查找绑定在元素上的事件吗? [英] Can I find events bound on an element with 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屋!