JavaScript中setAttribute怎么使用

在JavaScript中,setAttribute()方法用于给指定的元素设置一个指定的属性值。

使用语法:

element.setAttribute(attribute, value);

其中,

element是要设置属性的元素。

attribute是要设置的属性名称。

value是要设置的属性值。

示例:

var element = document.getElementById("myElement");
element.setAttribute("id", "newId");

上述示例中,首先通过getElementById()方法获取到id为"myElement"的元素,然后使用setAttribute()方法将其id属性值设置为"newId"。

阅读剩余
THE END