This question already has an answer here:
这个问题已经有了答案:
- jQuery find events handlers registered with an object 13 answers
- jQuery查找注册了对象13答案的事件处理程序
Tried to search online, but does not look like I can formulate search query properly.
尝试在网上搜索,但不像我可以合理地制定搜索查询。
So as simple as it sounds, how can I, either with jquery or just javascript list all the handlers or event listeners that are attached to element(s)/document/window or present in dom.
因此,尽管听起来很简单,但我如何使用jquery或javascript列出所有附加到元素(s)/文档/窗口或dom中的处理程序或事件侦听器。
Just wondering.
只是想知道。
Thank you in advance.
提前谢谢你。
4 个解决方案
#1
47
In jQuery before 1.8, try using $("#element").data("events")
在1.8之前的jQuery中,尝试使用$(“#元素”).data(“事件”)
EDIT:
编辑:
There is also jQuery extension: listHandlers
还有jQuery扩展:listhandler
#2
32
When debugging, if you want to just see if there's an event, I recommend using Visual Event or the Elements" section of Chrome's Developer Tools: select an element and look for "Event Listeners on the bottom right.
在调试时,如果您只想查看是否有一个事件,我建议使用Visual event或Chrome开发工具的“元素”部分:选择一个元素并在右下角查找“事件监听器”。
In your code, if you are using jQuery before version 1.8, you can use:
在您的代码中,如果在1.8版本之前使用jQuery,可以使用:
$(selector).data("events")
to get the events. As of version 1.8, this is discontinued (see this bug ticket). You can use:
的事件。在版本1.8中,此操作将停止(请参见此错误提示)。您可以使用:
$._data(element, "events")
but this is not recommended since it is an internal jQuery structure, and could change in future releases.
但是这是不推荐的,因为它是一个内部jQuery结构,并且可能在将来的版本中改变。
This question has some answers which may be useful, but none of them are particularly elegant in the same way that $(selector).data("events")
was.
这个问题有一些可能有用的答案,但是没有一个像$(选择器).data(“events”)那样优雅。
#3
16
Without jQuery:
没有jQuery:
if the listeners were added using elem.addEventListener() method, it is not easy to list these listeners. You can override the EventTarget.addEventListener() method by wrapping it with your own. Then you will have the information, what listeners were registered.
如果使用element . addeventlistener()方法添加侦听器,那么列出这些侦听器并不容易。可以使用自己的方法包装EventTarget.addEventListener()方法。然后你会得到信息,什么听众被注册了。
var f = EventTarget.prototype.addEventListener; // store original
EventTarget.prototype.addEventListener = function(type, fn, capture) {
this.f = f;
this.f(type, fn, capture); // call original method
alert('Added Event Listener: on' + type);
}
Working example you can find at http://jsfiddle.net/tomas1000r/RDW7F/
您可以在http://jsfiddle.net/tomas1000r/RDW7F/找到工作示例
#4
3
I just discovered visual event 2:
我刚刚发现了视觉事件2:
http://www.sprymedia.co.uk/article/Visual+Event+2
http://www.sprymedia.co.uk/article/Visual +事件+ 2
go under the "make it go section" and drag the text link to your bookmark toolbar go to a page that has events and click on the bookmark
转到“make it go”部分并将文本链接拖到书签工具栏,转到有事件的页面并单击书签
tested in FF Mac
测试在FF Mac
#1
47
In jQuery before 1.8, try using $("#element").data("events")
在1.8之前的jQuery中,尝试使用$(“#元素”).data(“事件”)
EDIT:
编辑:
There is also jQuery extension: listHandlers
还有jQuery扩展:listhandler
#2
32
When debugging, if you want to just see if there's an event, I recommend using Visual Event or the Elements" section of Chrome's Developer Tools: select an element and look for "Event Listeners on the bottom right.
在调试时,如果您只想查看是否有一个事件,我建议使用Visual event或Chrome开发工具的“元素”部分:选择一个元素并在右下角查找“事件监听器”。
In your code, if you are using jQuery before version 1.8, you can use:
在您的代码中,如果在1.8版本之前使用jQuery,可以使用:
$(selector).data("events")
to get the events. As of version 1.8, this is discontinued (see this bug ticket). You can use:
的事件。在版本1.8中,此操作将停止(请参见此错误提示)。您可以使用:
$._data(element, "events")
but this is not recommended since it is an internal jQuery structure, and could change in future releases.
但是这是不推荐的,因为它是一个内部jQuery结构,并且可能在将来的版本中改变。
This question has some answers which may be useful, but none of them are particularly elegant in the same way that $(selector).data("events")
was.
这个问题有一些可能有用的答案,但是没有一个像$(选择器).data(“events”)那样优雅。
#3
16
Without jQuery:
没有jQuery:
if the listeners were added using elem.addEventListener() method, it is not easy to list these listeners. You can override the EventTarget.addEventListener() method by wrapping it with your own. Then you will have the information, what listeners were registered.
如果使用element . addeventlistener()方法添加侦听器,那么列出这些侦听器并不容易。可以使用自己的方法包装EventTarget.addEventListener()方法。然后你会得到信息,什么听众被注册了。
var f = EventTarget.prototype.addEventListener; // store original
EventTarget.prototype.addEventListener = function(type, fn, capture) {
this.f = f;
this.f(type, fn, capture); // call original method
alert('Added Event Listener: on' + type);
}
Working example you can find at http://jsfiddle.net/tomas1000r/RDW7F/
您可以在http://jsfiddle.net/tomas1000r/RDW7F/找到工作示例
#4
3
I just discovered visual event 2:
我刚刚发现了视觉事件2:
http://www.sprymedia.co.uk/article/Visual+Event+2
http://www.sprymedia.co.uk/article/Visual +事件+ 2
go under the "make it go section" and drag the text link to your bookmark toolbar go to a page that has events and click on the bookmark
转到“make it go”部分并将文本链接拖到书签工具栏,转到有事件的页面并单击书签
tested in FF Mac
测试在FF Mac