Javascript document.body.addEventListener 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7167969/
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
document.body.addEventListener is not working
提问by Luke
I'm working on a screen reader project and I needed to get the paragraph under the cursor. To do that in Firefox, I wrote an extension, to which my screen reader connects through sockets and gets the text. I needed to get the current element under the cursor and the function worked fine in a sample html page written by myself (So there is no problem with the function).
我正在处理一个屏幕阅读器项目,我需要将段落置于光标下。为了在 Firefox 中做到这一点,我编写了一个扩展程序,我的屏幕阅读器通过套接字连接到该扩展程序并获取文本。我需要获取光标下的当前元素,并且该函数在我自己编写的示例 html 页面中运行良好(因此该函数没有问题)。
But when I try to attach the function to my JS file in the extension, It seems that the function which is called by "document.body.addEventListener('mouseover',myfunc(mEvent),false);" is never called. But if I call
但是当我尝试将函数附加到扩展中的 JS 文件时,似乎是由“document.body.addEventListener('mouseover',myfunc(mEvent),false);”调用的函数。永远不会被调用。但如果我打电话
document.body.addEventListener('mouseover',function(mEvent){...},true);
myfunc is called.
myfunc 被调用。
I'm new to javascript, so excuse me if it's a stupid question, But why the
我是 javascript 新手,所以如果这是一个愚蠢的问题,请原谅我,但是为什么
document.body.addEventListener('mouseover',function(mEvent){...},true);
is not working?
不管用?
I always appreciated your nice helps and ideas. :)
我一直很感激你的帮助和想法。:)
采纳答案by Wladimir Palant
While this isn't a direct answer to your question, I hope that it is helpful nevertheless. Using an extension for a screen reader is unnecessary, in particular you might not want to update that extension regularly as the browser's user interface changes. Firefox supports a number of accessibility APIswhich you can use directly from your application. These APIs have the advantage of being stable, once your implementation is done it should continue working (as far as I remember there was only one occasion where Mozilla broke compatibility with existing screen readers).
虽然这不是对您问题的直接回答,但我希望它仍然有帮助。无需为屏幕阅读器使用扩展程序,尤其是您可能不希望随着浏览器用户界面的变化而定期更新该扩展程序。Firefox 支持许多可访问性 API,您可以直接从您的应用程序中使用它们。这些 API 的优点是稳定,一旦您的实现完成,它应该继续工作(据我所知,只有一次 Mozilla 破坏了与现有屏幕阅读器的兼容性)。
As to the actual question: you didn't tell anything about the context in which this code is executing. Normally, extensions run code in their XUL overlay meaning that the context is the browser window, not the web page. Which would explain why your code isn't working (it's a XUL document, no document.body
there). What you probably mean is attaching an event listener to the <tabbrowser>
element where the web pages are loaded: gBrowser.addEventListener(...)
. Side-note: If you really wanted to catch all events on a XUL document you should register an event listener on document
or document.documentElement
.
至于实际问题:您没有说明有关此代码执行的上下文的任何信息。通常,扩展在它们的 XUL 覆盖中运行代码,这意味着上下文是浏览器窗口,而不是网页。这将解释为什么您的代码不起作用(它是 XUL 文档,没有document.body
)。你大概的意思是事件侦听器连接到<tabbrowser>
在网页中加载元素:gBrowser.addEventListener(...)
。旁注:如果您真的想捕获 XUL 文档上的所有事件,您应该在document
或上注册一个事件侦听器document.documentElement
。
Btw, you can see error messages related to extensions in the Error Console(the article I link to explains how to enable it in the current Firefox versions).
顺便说一句,您可以在错误控制台中看到与扩展相关的错误消息(我链接到的文章解释了如何在当前的 Firefox 版本中启用它)。
回答by Luke
There may be other issues here, but the syntax of your addEventListener
call is incorrect and could be causing the issue you are seeing:
这里可能还有其他问题,但您addEventListener
调用的语法不正确,可能会导致您看到的问题:
document.body.addEventListener('mouseover',myfunc(mEvent),false);
is actually invoking "myfunc" at the same time you are invoking addEventListener
and passing it as the second parameter.
document.body.addEventListener('mouseover',myfunc(mEvent),false);
实际上是在调用“myfunc”的同时调用addEventListener
它并将其作为第二个参数传递。
You should instead be calling it this way:
你应该这样称呼它:
document.body.addEventListener('mouseover',function() { myfunc(mEvent) },false);
document.body.addEventListener('mouseover',function() { myfunc(mEvent) },false);
回答by user2347127
I also had the same problem and now solved it. After closing tag, try adding an document.body.addEventListener
. Parser didn't recognize DOM object of the "addEventListener" event because HTML tag of DOM object is not closed.
我也遇到了同样的问题,现在解决了。关闭标签后,尝试添加一个document.body.addEventListener
. 解析器无法识别“addEventListener”事件的 DOM 对象,因为 DOM 对象的 HTML 标记未关闭。
Refer to following. It works well.
请参阅以下内容。它运作良好。
<html>
<body>
...
<\body>
<script type="text/javascript">
document.body.addEventListener('click', function() { ... });
</script>
</html>
回答by Hocker Fun
following Wladimir Palant's advise, the follow is working:
按照弗拉基米尔·帕兰特的建议,以下工作有效:
document.addEventListener('click', function() { ... }, false);
回答by mrtsherman
I suspect that the document is not yet ready, so there is nothing to bind to. Try adding an onload="addMyEventListener();"
to your <body>
and see if that fixes things up. I don't know the proper way to time things, so maybe someone else can chime in with the best way to handle this.
我怀疑文档还没有准备好,所以没有什么可以绑定的。尝试添加一个onload="addMyEventListener();"
到你的<body>
,看看是否能解决问题。我不知道给事情计时的正确方法,所以也许其他人可以用最好的方法来处理这个问题。