setAttribute()
时间: 2024-05-10 11:21:41 浏览: 54
JSP request.setAttribute()详解及实例
5星 · 资源好评率100%
The setAttribute() method is used to set the value of an attribute on a selected HTML element. It takes two parameters: the name of the attribute and the value to be assigned to it.
For example, the following code sets the "src" attribute of an image element to "picture.jpg":
```
var myImage = document.getElementById("image1");
myImage.setAttribute("src", "picture.jpg");
```
This method can be used to set any attribute on an HTML element, not just the "src" attribute on an image. For example, it can be used to set the "href" attribute on an anchor element or the "class" attribute on any element.
It's important to note that if the attribute already exists on the element, the setAttribute() method will overwrite the existing value. If the attribute doesn't exist, it will be created and assigned the value passed in.
阅读全文