Skip to main content

getAttribute()

设置指定元素上的某个属性值。如果属性已经存在,则更新该值;否则,使用指定的名称和值添加一个新的属性。

要获取某个属性当前的值,可使用 getAttribute();要删除某个属性,可使用 removeAttribute()

语法

element.setAttribute(name, value);

示例

在下面的例子中,setAttribute() 被用于设置 <button> 上的属性。

HTML

<button>Hello World</button>

JavaScript

const b = document.querySelector("button");

b.setAttribute("name", "helloButton");
b.setAttribute("disabled", "");