简体   繁体   English

Firefox扩展程序无法覆盖元素事件

[英]Firefox Extension cannot override element events

I'm having trouble trying to override a form element's onsubmit event. 我在尝试覆盖表单元素的onsubmit事件时遇到麻烦。 I have no problem adding a listener with addEventListener , but for my particular case, I need to replace the onsubmit but for some reason when I do, it gives me this error: 我没有用addEventListener添加侦听器的问题,但是对于我的特殊情况,我需要替换 onsubmit,但是由于某种原因,它给了我这个错误:

Error: Component is not available = NS_ERROR_NOT_AVAILABLE

My code is simply this: 我的代码就是这样:

gBrowser.contentDocument.getElementById("theform").onsubmit = function() {
   return false;
};

Essentially I want to prevent the form from submitting, but this code fails and throws the above error. 本质上,我想阻止表单提交,但是此代码失败并引发上述错误。 Using addEventListener to return false doesn't seem to stop the form from submitting. 使用addEventListener返回false似乎不会阻止表单提交。

Thanks. 谢谢。

For security reasons, the object returned by getElementById in an extension is an XPCNativeWrapper around the DOM element; 出于安全考虑,扩展中的getElementById返回的对象是DOM元素周围的XPCNativeWrapper it's not the element itself. 这不是元素本身。 This results in some important limitations. 这导致一些重要的限制。 More details here: 此处有更多详细信息:

Assigning to or reading an on* property on an XPCNativeWrapper of a DOM node or Window object will throw an exception. 在DOM节点或Window对象的XPCNativeWrapper上分配或读取on *属性将引发异常。 (Use addEventListener instead, and use "event.preventDefault();" in your handler if you used "return false;" before.) (如果以前使用“ return false;”,则使用addEventListener,并在处理程序中使用“ event.preventDefault();”。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM