Node: lookupPrefix() 方法

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

lookupPrefix() 方法是 Node 介面的一部分,用於返回給定名稱空間 URI 的字首(如果存在),如果不存在則返回 null。當存在多個可能的字首時,將返回第一個字首。

語法

js
lookupPrefix(namespace)

引數

namespace

一個字串,表示要查詢字首的名稱空間。

注意:此引數不是可選的,但可以設定為 null

返回值

一個字串,包含相應的名稱空間字首;如果沒有找到,則返回 null。如果 namespace 為 null 或空字串,則 lookupPrefix() 返回 null

如果節點是 DocumentTypeDocumentFragmentlookupPrefix() 始終返回 null

示例

html
Prefix for <code>http://www.w3.org/2000/svg</code> on &lt;output&gt;:
<output>Not tested</output>.<br />
Prefix for <code>http://www.w3.org/XML/1998/namespace</code> on &lt;output&gt;:
<output>Not tested</output>.<br />
Prefix for <code>http://www.w3.org/TR/html4/</code> on &lt;output&gt;:
<output>Not tested</output>.<br />
Prefix for <code>https://www.w3.org/1999/xlink</code> on &lt;output&gt;:
<output>Not tested</output>.<br />
Prefix for <code>http://www.w3.org/2000/svg</code> on &lt;svg&gt;:
<output>Not tested</output>.<br />
Prefix for <code>https://www.w3.org/1999/xlink</code> on &lt;svg&gt;:
<output>Not tested</output>.<br />
Prefix for <code>http://www.w3.org/XML/1998/namespace</code> on &lt;svg&gt;:
<output>Not tested</output>.<br />
<svg xmlns:t="http://www.w3.org/2000/svg" height="1"></svg>
<button>Click to see the results</button>
js
const button = document.querySelector("button");
button.addEventListener("click", () => {
  const htmlElt = document.querySelector("output");
  const svgElt = document.querySelector("svg");

  const result = document.getElementsByTagName("output");
  result[0].value = htmlElt.lookupPrefix("http://www.w3.org/2000/svg"); // true
  result[1].value = htmlElt.lookupPrefix(
    "http://www.w3.org/XML/1998/namespace",
  ); // false
  result[2].value = htmlElt.lookupPrefix("http://www.w3.org/TR/html4/"); // true
  result[3].value = htmlElt.lookupPrefix("https://www.w3.org/1999/xlink"); // false
  result[4].value = svgElt.lookupPrefix("http://www.w3.org/2000/svg"); // true
  result[5].value = svgElt.lookupPrefix("https://www.w3.org/1999/xlink"); // true
  result[6].value = svgElt.lookupPrefix("http://www.w3.org/XML/1998/namespace"); // false
});

規範

規範
DOM
# dom-node-lookupprefix

瀏覽器相容性