Thanks to visit codestin.com
Credit goes to developer.mozilla.org

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Element:hasAttributeNS() 方法

基线 广泛可用

自 2015年7月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

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");
}

规范

规范
DOM
# ref-for-dom-element-hasattributens①

浏览器兼容性

参见