如何检查是否有任何 JavaScript 事件侦听器/处理程序附加到元素/文档?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2382994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:01:38  来源:igfitidea点击:

How to check if any JavaScript event listeners/handlers attached to an element/document?

javascriptjquerydom-events

提问by GnrlBzik

Tried to search online, but does not look like I can formulate search query properly.

尝试在线搜索,但看起来我无法正确制定搜索查询。

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 列出附加到元素/文档/窗口或存在于 DOM 中的所有处理程序或事件侦听器?

采纳答案by Tomas Jakubco

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.

如果监听器是使用elem.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/找到工作示例

回答by Laimoncijus

In jQuery before 1.8, try using $("#element").data("events")

在 jQuery 1.8 之前,尝试使用 $("#element").data("events")

EDIT:

编辑:

There is also jQuery extension: listHandlers

还有 jQuery 扩展:listHandlers

回答by Luke

When debugging, if you want to just see if there's an event, I recommend using Visual Eventor the Elements" section of Chrome's Developer Tools: select an element and look for "Event Listenerson the bottom right.

调试时,如果只想查看是否有事件,我建议使用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 questionhas some answers which may be useful, but none of them are particularly elegant in the same way that $(selector).data("events")was.

这个问题有一些可能有用的答案,但没有一个像$(selector).data("events")以前那样特别优雅。

回答by Mark

I just discovered visual event 2:

我刚刚发现了视觉事件 2:

http://www.sprymedia.co.uk/article/Visual+Event+2

http://www.sprymedia.co.uk/article/Visual+Event+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

转到“让它运行部分”下并将文本链接拖到书签工具栏转到包含事件的页面并单击书签

tested in FF Mac

在 FF Mac 中测试