javascript Firebug:如何查看对象的附加事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4214610/
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
Firebug: How to see attached events of an object?
提问by Florian Müller
I've attached some events to some divs using addEventListener. But where can I see the events in Firebug?
我已经使用addEventListener. 但是我在哪里可以看到 Firebug 中的事件?
回答by Orbling
回答by Sam
It's probably worth mentioning that Firebug 1.12 introduced getEventListeners(target). The Firebug wiki page for it is here, and there's a very useful blog post about it here.
值得一提的是,Firebug 1.12 引入了getEventListeners(target). 它Firebug的wiki页面是在这里,而且有一个非常有用的博客文章关于它在这里。
(Firebug 1.12 was only released in August 2013, so the answer to this question was right when it was originally posted.)
(Firebug 1.12 仅在 2013 年 8 月发布,所以这个问题的答案在最初发布时是正确的。)
However, there are a couple of caveats for getEventListeners:
但是,有几个注意事项getEventListeners:
First off, it won't work if you pass it a jQuery object; pass it a normal DOM object instead. (Perhaps this is obvious, but it caught me out!)
首先,如果你传递给它一个 jQuery 对象,它就不会工作;而是传递一个普通的 DOM 对象。(也许这很明显,但它让我措手不及!)
Secondly, I've found that getEventListenersdoesn't always work if I run it before all code on a page has loaded. I'm not sure exactly when it does and doesn't work, but I've certainly seen a situation like this:
其次,我发现getEventListeners如果在页面上的所有代码加载之前运行它,它并不总是有效。我不确定它什么时候起作用,什么时候不起作用,但我肯定见过这样的情况:
>>> getEventListeners(document.getElementById('elementid'))
ReferenceError: getEventListeners is not defined
>>> $._data(document.getElementById('elementid'), "events");
Object { click=[1]}
As you can see, the "longhand" method (from the SO post linked to in the answer) can retrieve the event, but getEventListenersis showing up as not defined. This error is different to the return value you get if getEventListenersruns but reports that an object has no listeners, so I'd say you can use getEventListenerswithout fear, as it'll be obvious if it's not yet available!
如您所见,“普通”方法(来自答案中链接到的 SO 帖子)可以检索事件,但getEventListeners显示为未定义。此错误与您在getEventListeners运行时获得的返回值不同,但报告对象没有侦听器,因此我会说您可以getEventListeners放心使用,因为如果尚不可用,则很明显!

