How to use document.getElementById(id) in JavaScript?
The document.getElementById(id) method is used to retrieve the element with the specified id attribute. To use this method, follow the steps below:
- Set a unique id attribute for the element you want to retrieve in the HTML file, as shown below:
<div id="myElement">这是一个示例元素</div>
- In a JavaScript file, use the document.getElementById(id) method to retrieve the element as shown below:
var element = document.getElementById("myElement");
- constituent
element.innerHTML = "这是修改后的内容";
element.style.color = "red";
By following the above steps, you can use the document.getElementById(id) method to retrieve and manipulate the specified element with the given id.