Firefox 扩展无法覆盖元素事件

发布于 2024-12-06 18:59:11 字数 497 浏览 4 评论 0原文

我在尝试覆盖表单元素的 onsubmit 事件时遇到问题。我使用 addEventListener 添加监听器没有问题,但对于我的特殊情况,我需要替换 onsubmit,但由于某种原因,当我这样做时,它给了我这个错误:

Error: Component is not available = NS_ERROR_NOT_AVAILABLE

我的代码很简单:

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

本质上我想阻止表单提交,但是此代码失败并引发上述错误。使用 addEventListener 返回 false 似乎并没有阻止表单提交。

谢谢。

I'm having trouble trying to override a form element's onsubmit event. 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:

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.

Thanks.

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

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

发布评论

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

评论(1

月亮坠入山谷 2024-12-13 18:59:11

出于安全原因,扩展中的 getElementById 返回的对象是 DOM 元素周围的 XPCNativeWrapper;它不是元素本身。这导致了一些重要的限制。更多详细信息请参见此处:

分配或读取 DOM 节点或 Window 对象的 XPCNativeWrapper 上的 on* 属性将引发异常。 (请改用 addEventListener,如果您之前使用过“return false;”,请在处理程序中使用“event.preventDefault();”。)

For security reasons, the object returned by getElementById in an extension is an XPCNativeWrapper around the DOM element; 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. (Use addEventListener instead, and use "event.preventDefault();" in your handler if you used "return false;" before.)

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