Node:isDefaultNamespace() 方法

Baseline 已廣泛支援

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

Node 介面的 isDefaultNamespace() 方法接受一個名稱空間 URI 作為引數。如果名稱空間是給定節點上的預設名稱空間,則返回 true,否則返回 false

注意: HTML 元素的預設名稱空間始終是 ""。對於 SVG 元素,它由 xmlns 屬性設定。

語法

js
isDefaultNamespace(namespaceURI)

引數

namespaceURI

一個字串,表示將用於檢查元素的名稱空間。

注意: namespaceURI 不是可選引數,但可以為 null

返回值

一個布林值,包含返回值 truefalse,指示引數是否為預設名稱空間。

示例

html
Is "" the default namespace for <output>:
<output>Not tested</output>.<br />
Is "http://www.w3.org/2000/svg" the default namespace for &lt;output&gt;:
<output>Not tested</output>.<br />
Is "" the default namespace for &lt;svg&gt;: <output>Not tested</output>.<br />
Is "http://www.w3.org/2000/svg" the default namespace for &lt;svg&gt;:
<output>Not tested</output>.<br />
<svg xmlns="http://www.w3.org/2000/svg" height="1"></svg>
<button>Click to run tests</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.isDefaultNamespace(""); // true
  result[1].value = htmlElt.isDefaultNamespace("http://www.w3.org/2000/svg"); // false
  result[2].value = svgElt.isDefaultNamespace(""); // false
  result[3].value = svgElt.isDefaultNamespace("http://www.w3.org/2000/svg"); // true
});

規範

規範
DOM
# dom-node-isdefaultnamespace

瀏覽器相容性

另見