可以通过 javascript 设置 mouseover html 属性,但无法在 Firefox 中为 mouseover 属性分配功能

发布于 2024-12-12 16:08:32 字数 1422 浏览 1 评论 0原文

显然我在做一些愚蠢的事情。

    processTextNodes: function processTextNodes(node) { 
    node = node || content.document.body;                    // base node 
    var children = node.childNodes, i = 0;

    while (node = children[i]) { 
        if (node.nodeType == 3 && node.textContent) {        // text node found, replace enclosed text.
            if (node.nodeName == "script") continue;

            /*node.parentNode.setAttribute("onmouseover", "alert(\"AAA\");");*/
            node.parentNode.onmouseover = function(){ alert("AAA") };
            node.textContent = aatel.transliterate(node.textContent);
        }
        if (node.nodeType == 1) {
            if (node.title) node.title = aatel.transliterate(node.title);
            if (node.alt)   node.alt = aatel.transliterate(node.alt);
        }
        processTextNodes(node); 
        i++; 
    }
},

上面是我正在编写的扩展中的方法。当按照上面给出的方式运行时,我在错误控制台中得到以下内容

Error: uncaught exception: [Exception... "Component is not available"  nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)"  location: "JS frame :: chrome://aatel/content/overlay.js :: processTextNodes :: line 97"  data: no]

如果有任何歧义,第 97 行是:

node.parentNode.onmouseover = function(){ alert("AAA")};

如果我注释掉将函数分配给 .onmouseover 的行并取消注释其上方的 setAttribute 行,它会按您的预期工作。大而丑陋的不透明异常消息对我没有多大帮助。我做错了什么? JavaScript 不是我的强项。

Clearly I'm doing something stupid.

    processTextNodes: function processTextNodes(node) { 
    node = node || content.document.body;                    // base node 
    var children = node.childNodes, i = 0;

    while (node = children[i]) { 
        if (node.nodeType == 3 && node.textContent) {        // text node found, replace enclosed text.
            if (node.nodeName == "script") continue;

            /*node.parentNode.setAttribute("onmouseover", "alert(\"AAA\");");*/
            node.parentNode.onmouseover = function(){ alert("AAA") };
            node.textContent = aatel.transliterate(node.textContent);
        }
        if (node.nodeType == 1) {
            if (node.title) node.title = aatel.transliterate(node.title);
            if (node.alt)   node.alt = aatel.transliterate(node.alt);
        }
        processTextNodes(node); 
        i++; 
    }
},

The above is a method from an extension I'm writing. When run as given above I get the following in the error console

Error: uncaught exception: [Exception... "Component is not available"  nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)"  location: "JS frame :: chrome://aatel/content/overlay.js :: processTextNodes :: line 97"  data: no]

In case there's any ambiguity, line 97 is:

node.parentNode.onmouseover = function(){ alert("AAA")};

If I comment out the line assigning a function to .onmouseover and uncomment the setAttribute line right above it, it works as you would expect. The big ugly opaque exception message is not helping me a lot. What am I doing wrong? Javascript isn't a strong point of mine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

り繁华旳梦境 2024-12-19 16:08:32

您似乎正在尝试在 Firefox 扩展中使用事件。尝试使用 addEventListener 添加事件:

node.parentNode.addEventListener("mouseover", function () {
    alert("AAA");
}, false);

Looks like you're trying to use events in a Firefox exension. Try using addEventListener for adding events:

node.parentNode.addEventListener("mouseover", function () {
    alert("AAA");
}, false);
奈何桥上唱咆哮 2024-12-19 16:08:32

onmouseover setter 做出了某些假设。例如,它假设调用它的脚本正在针对 Window 对象运行。如果不是这种情况,它将抛出。

如果上面的脚本位于扩展中的 JS 组件中,则它不会针对 Window 运行...

The onmouseover setter makes certain assumptions. For example, it assumes that the script calling it is running against a Window object. If that's not the case, it will throw.

If your script above is in a JS component in the extension, it's not running against a Window...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文