2010-09-13 60 views
3

我是TryAgain的Firefox开发者之一,它在网站加载失败时显示自定义错误页面。它本质上取代了Firefox的netError.xhtml自定义版本。XUL按钮不出现

但是,我遇到了3.0 * .3.6。*和Fx4b5之间的一些相当的终端兼容性问题。 (在netError.dtd的条目已更名,导致任何一个版本或其他一个XML解析错误。)

为了解决这个问题,我已经决定要具有扩展动态修改页面,反对完全替换它。我需要在Fx3中添加到netError.xhtml的一个元素是<xul:button>。然而,用下面的代码添加它,没有出现在屏幕上:

var div = document.getElementById("errorContent"); 
var btn = document.createElement("xul:button"); 
btn.setAttribute("label", "Hello world"); 
btn.setAttribute("oncommand", "alert('Hello world!');"); 
div.appendChild(btn); 

我看到在Mozilla开发者中心是there is this note

Gecko implementation of createElement doesn't conform to the DOM spec for XUL and XHTML documents: localName and namespaceURI are not set to null on the created element. See bug 280692 for details.

这是什么继承权问题,以及哪能解决它?

此外,如何通过JavaScript执行oncommand事件?

+1

你试过用createElementNS(” http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul“,”xul:button“)? – lithorus 2010-09-14 22:05:30

回答

1

document.createElement()不接受限定名称。您传递的“xul:button”字符串使其创建一个名为“xul:button”(== localName)的元素,而不是XUL“按钮”元素。另一方面,当解析XML时,< xul:button>被解析为限定名称:解析器搜索与xul前缀对应的名称空间(从其中一个父元素中的xmlns:xul=""定义),并创建“按钮“元素找到它的名字空间。

关于不符合XUL和(X)HTML的DOM规范的说明意味着您可以使用常规document.createElement("buttton")相应地在XUL或HTML文档的XUL或HTML命名空间中创建元素。

或者你可以去硬盘的方式及用途:

var XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; 
document.createElementNS(XUL_NS, "button") 

甚至与合格的名称,而不是有任何理由这样做:

document.createElementNS(XUL_NS, "xul:button") 
+0

感谢您的解释。这是有道理的;我会尽快给它摆动。 – 2010-10-25 22:57:59

+0

果然,这是个诀窍。谢谢! – 2010-11-04 23:51:39

+0

我一直在[接收报告](http://getsatisfaction.com/tryagain/topics/tryagain_error?utm_content=topic_link&utm_medium=email&utm_source=new_topic)该代码引发以下错误:'错误:组件返回失败代码:0x80040111 (NS_ERROR_NOT_AVAILABLE)[nsIDOMHTMLDocument.createElementNS]'。似乎Firefox偶尔无法访问命名空间服务器? – 2011-01-30 12:54:05