Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

javascript - 我可以使用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:(JavaScript:)

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

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

Is there any way to get a list of all events bound on an element, in this case on element with id="elm" ?(有什么方法可以获取绑定在元素上的所有事件的列表,在本例中是id="elm"元素?)

  ask by Praveen Prasad translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In modern versions of jQuery, you would use the $._data method to find any events attached by jQuery to the element in question.(在现代版本的jQuery中,您将使用$._data方法查找jQuery附加到相关元素的任何事件。)

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" );

The result from $._data will be an object that contains both of the events we set (pictured below with the mouseout property expanded):($._data的结果将是一个包含我们设置的两个事件的对象(如下图所示,其中的mouseout属性已展开):)

$ ._的控制台输出

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.(然后,在Chrome中,您可以右键单击处理函数,然后单击“查看函数定义”,以向您显示在代码中定义的确切位置。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...