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①

浏览器兼容性

另见