可以通过 javascript 设置 mouseover html 属性,但无法在 Firefox 中为 mouseover 属性分配功能
显然我在做一些愚蠢的事情。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎正在尝试在 Firefox 扩展中使用事件。尝试使用
addEventListener
添加事件:Looks like you're trying to use events in a Firefox exension. Try using
addEventListener
for adding events: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...