Node: lookupNamespaceURI() 方法
Node 接口的 lookupNamespaceURI() 方法以一个前缀作为参数,并在找到时返回与给定节点上该前缀关联的命名空间 URI(如果未找到则返回 null)。该方法的存在允许将 Node 对象作为命名空间解析器传递给 XPathEvaluator.createExpression() 和 XPathEvaluator.evaluate()。
语法
js
lookupNamespaceURI(prefix)
参数
prefix-
要查找的前缀。
注意: 此参数不是可选的,但可以设置为
null。
返回值
与前缀对应的命名空间 URI 字符串。
- 如果节点是
DocumentFragment、DocumentType、没有documentElement的Document,或者没有关联元素的Attr,则始终返回null。 - 如果
prefix是"xml",则返回值始终为"http://www.w3.org/XML/1998/namespace"。 - 如果
prefix是"xmlns",则返回值始终为"http://www.w3.org/2000/xmlns/"。 - 如果
prefix为null,则返回值为默认命名空间 URI。 - 如果未找到该前缀,则返回值为
null。
示例
html
<div class="hidden">
<div>Test HTML element</div>
<svg>
<text>Test SVG element</text>
</svg>
<math>Test MathML element</math>
</div>
<table>
<thead>
<tr>
<th><code>prefix</code></th>
<th><code><div></code></th>
<th><code><svg></code></th>
<th><code><math></code></th>
</tr>
</thead>
<tbody></tbody>
</table>
js
const htmlElt = document.querySelector("div");
const svgElt = document.querySelector("svg");
const mathElt = document.querySelector("math");
const tbody = document.querySelector("tbody");
for (const prefix of ["xmlns", "xml", "html", "svg", "xlink", "", null]) {
const row = document.createElement("tr");
tbody.appendChild(row);
row.appendChild(document.createElement("td")).textContent =
JSON.stringify(prefix);
for (const el of [htmlElt, svgElt, mathElt]) {
console.log(el, prefix, el.lookupNamespaceURI(prefix));
row.appendChild(document.createElement("td")).textContent = String(
el.lookupNamespaceURI(prefix),
);
}
}
规范
| 规范 |
|---|
| DOM # dom-node-lookupnamespaceuri |
浏览器兼容性
加载中…