Before operating on DOM nodes, you must first obtain the DOM nodes. There are many ways to obtain DOM nodes, which can be obtained based on the id attribute and tag name.
getElementById() method
To obtain DOM nodes based on the id attribute of the HTML tag, please use the getElementById() method. This method returns a node object.
Syntax: document.getElementById(id) where id is the id attribute of the HTML tag.
For example, the statement to obtain the node with id="demo" is: document.getElementById("demo");
getElementsByTagName() method
According to HTML To get the DOM node by tag name, please use the getElementsByTagName() method. This method returns the obtained element node as an array.
Syntax: nodeObject.getElementsByTagName(tagName)
Among them, nodeObject is the element node and tagName is the name of the HTML tag.
Note: The getElementsByTagName() method can not only search all nodes in the entire HTML document, but also search the child nodes of a certain node. When using it, you must specify the search range, that is, specify nodeObject.
For example, get all the
document.getElementsByTagName("div");
Get the id="demo" All
document.getElementById("demo").getElementsByTagName("div");