测试事件处理程序是否绑定到jQuery中的元素 [英] test if event handler is bound to an element in jQuery

查看:84
本文介绍了测试事件处理程序是否绑定到jQuery中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以确定元素是否具有单击处理程序,或更改处理程序,或使用jQuery绑定到它的任何类型的事件处理程序?

Is it possible to determine whether an element has a click handler, or a change handler, or any kind of event handler bound to it using jQuery?

此外,是否可以确定它对于给定类型的事件有多少点击处理程序(或任何类型的事件处理程序),以及事件处理程序中有哪些函数?

Furthermore, is it possible to determine how many click handlers (or whatever kind of event handlers) it has for a given type of event, and what functions are in the event handlers?

推荐答案

您可以从数据缓存中获取此信息。

You can get this information from the data cache.

例如,将它们记录到控制台(firebug,ie8):

For example, log them to the console (firebug, ie8):

console.dir( $('#someElementId').data('events') );

或迭代它们:

jQuery.each($('#someElementId').data('events'), function(i, event){

    jQuery.each(event, function(i, handler){

        console.log( handler.toString() );

    });

});

另一种方法是使用以下 bookmarklet 但显然这在运行时没有帮助。

Another way is you can use the following bookmarklet but obviously this does not help at runtime.

这篇关于测试事件处理程序是否绑定到jQuery中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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