--- title: Element.namespaceURI slug: Web/API/Element/namespaceURI translation_of: Web/API/Element/namespaceURI --- <div>{{APIRef("DOM")}}</div> <p><code><strong>Element.namespaceURI</strong></code> 是一个只读属性,它返回元素的命名空间,若该元素不在命名空间中则返回<code>null</code> .</p> <div class="note"> <p>在DOM4之前, 这个 API 在接口 {{domxref("Node")}} 中定义 .</p> </div> <h2 id="语法">语法</h2> <pre class="syntaxbox"><var>namespace</var> = <var>element</var>.namespaceURI</pre> <h2 id="例子">例子</h2> <p>在这段代码中,我们检查了元素的{{domxref("localName")}}和<code>namespaceURI</code>。如果 <code>namespaceURI</code> 返回 XUL 命名空间, <code>localName</code> 返回"browser",于是这个节点被理解为是一个XUL <code><browser/></code>。</p> <pre class="brush:js">if (element.localName == "browser" && element.namespaceURI == "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul") { // this is a XUL browser }</pre> <h2 id="Notes" name="Notes">注意</h2> <p>这不是一个计算值,它是基于范围内的名称空间声明检查的名称空间查找的结果。节点命名空间在节点创建时被冻结。</p> <p>在Firefox 3.5 以及之前的版本, HTML文档中的HTML元素的名称空间URI为 <code>null</code>。 在更早的版本中, 符合HTML5, 它是<code><a class="external" href="http://www.w3.org/1999/xhtml" rel="freelink">http://www.w3.org/1999/xhtml</a></code> 如 XHTML。{{gecko_minversion_inline("1.9.2")}}</p> <p>您可以使用DOM Level 2方法指定的<code>namespaceURI</code>创建一个元素 <a href="/en-US/docs/Web/API/Document/createElementNS" title="Document.createElementNS">document.createElementNS</a>。</p> <p>DOM本身不处理或执行名称空间验证。 它由DOM应用程序完成,以执行任何必要的验证。注意,名称空间前缀一旦与某个特定元素相关联,就不能更改。</p> <h2 id="说明">说明</h2> <table class="standard-table"> <thead> <tr> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> </thead> <tbody> <tr> <td>{{SpecName("DOM4", "#dom-element-namespaceuri", "Element.namespaceuri")}}</td> <td>{{Spec2("DOM4")}}</td> <td>Initial definition</td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> {{Compat("api.Element.namespaceURI")}} <h2 id="See_also">See also</h2> <ul> <li>{{domxref("Element.localName")}}</li> <li>{{domxref("Element.prefix")}}</li> <li>{{domxref("Attr.namespaceURI")}}</li> <li>{{domxref("Node.namespaceURI")}}</li> </ul>