document.body.addEventListener不起作用 [英] document.body.addEventListener is not working

查看:1502
本文介绍了document.body.addEventListener不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个屏幕阅读器项目,我需要在光标下面找到段落。为了在Firefox中做到这一点,我写了一个扩展,我的屏幕阅读器通过套接字连接到该扩展并获取文本。我需要在光标下获取当前元素,并且该函数在我自己编写的示例html页面中工作正常(因此该函数没有问题)。

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).

但是当我尝试将函数附加到扩展中的JS文件时,似乎是由document.body.addEventListener('mouseover',myfunc(mEvent),false)调用的函数;永远不会被称为。
但是如果我打电话

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

document.body.addEventListener('mouseover',function(mEvent){...},true);

调用myfunc。

我'我是javascript的新手,请原谅我这是一个愚蠢的问题,但为什么

I'm new to javascript, so excuse me if it's a stupid question, But why the

document.body.addEventListener('mouseover',function(mEvent){...},true);

无效?

我总是感谢您的帮助和想法。 :)

I always appreciated your nice helps and ideas. :)

推荐答案

虽然这不是你问题的直接答案,但我希望它仍然有用。不需要使用屏幕阅读器的扩展,特别是当浏览器的用户界面发生变化时,您可能不希望定期更新该扩展。 Firefox支持许多辅助功能API ,您可以直接在您的应用程序中使用它们。这些API具有稳定的优势,一旦您的实现完成它应该继续工作(据我记得只有一个场合,Mozilla破坏了与现有屏幕阅读器的兼容性)。

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 APIs which 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).

关于实际问题:您没有告诉任何有关此代码执行的上下文的信息。通常,扩展在其XUL覆盖中运行代码,这意味着上下文是浏览器窗口,而不是网页。这可以解释为什么你的代码不起作用(这是一个XUL文档,没有 document.body 那里)。您可能的意思是将事件监听器附加到加载网页的< tabbrowser> 元素: gBrowser.addEventListener(...) 。附注:如果你真的想要捕获XUL文档上的所有事件,你应该在 document document.documentElement

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.

顺便说一句,您可以在错误控制台(我链接的文章解释了如何在当前的Firefox版本中启用它。)

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).

这篇关于document.body.addEventListener不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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