Element:hasAttributeNS() 方法

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Element 接口的 hasAttributeNS() 方法返回一个表示当前元素是否具有指定命名空间的指定属性的布尔值。

如果你正在处理 HTML 文档,并且你不需要指定属性是否属于特定命名空间,请改用 hasAttribute() 方法。

语法

js
hasAttributeNS(namespace,localName)

参数

  • namespace:一个指定属性的命名空间的字符串。
  • localName:属性的名称。

返回值

一个布尔值。

示例

js
// 在为属性设置值之前检测其是否存在
const d = document.getElementById("div1");
if (
  d.hasAttributeNS("http://www.mozilla.org/ns/specialspace/", "special-align")
) {
  d.setAttribute("align", "center");
}

规范

Specification
DOM
# ref-for-dom-element-hasattributens①

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
hasAttributeNS

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见