DOM
Element.hasAttribute() 方法返回一个表示当前元素的指定属性是否存在的布尔值。
语法
hasAttribute(name)
参数
name- : 表示属性名称的字符串。
返回值
一个布尔值。
示例
const foo = document.getElementById("foo");
if (foo.hasAttribute("bar")) {
// 做一些事情
}
规范
浏览器兼容性
参见
- Element.hasAttributes()
- Element.getAttribute()
- Element.setAttribute()
- Element.removeAttribute()
- Element.toggleAttribute()
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
