Element:hasAttributeNS() 方法

Baseline 已廣泛支援

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

hasAttributeNS() 方法是 Element 介面的一部分,它返回一個布林值,指示當前元素是否具有指定名稱空間中具有指定名稱的屬性。

如果您正在處理 HTML 文件,並且不需要將請求的屬性指定為特定名稱空間的一部分,請改用 hasAttribute() 方法。

語法

js
hasAttributeNS(namespace,localName)

引數

namespace

一個指定屬性名稱空間的字串。

localName

屬性的名稱。

返回值

布林值。

示例

js
// Check that the attribute exists before you set a value
const d = document.getElementById("div1");
if (
  d.hasAttributeNS("http://www.mozilla.org/ns/specialspace/", "special-align")
) {
  d.setAttribute("align", "center");
}

規範

規範
DOM
# ref-for-dom-element-hasattributens①

瀏覽器相容性

另見