HTML dom Access nodes
With the Dom, you can access every node in an HTML document.(With Dom, you can access every node in the HTML document)
Accessing nodes
You can access a node in three ways:(You can use three methods to access the node)
- By using the getelementbyid () method(Use the getelementbyid method)
- By using the getelementsbytagname () method(By using the getelementsbytagname method)
- By navigating the node tree, using the node relationship(Use the relationship between nodes to navigate the node tree)
Supplement: 1. Besides using getelementsbytagname, you can also use the getelementsbyname method to access the nodes.
2. In addition to the ID attribute, you can also use the classname attribute to access the node.
Example:
<SCRIPT type = "text/JavaScript">
// <! --
Window. onload = function (){
VaR I, A, S = "";
VaR d = Document. getelementsbytagname ("Div ");// Obtain the HTML elements whose Tag Name Is Div
For (I = 0; I <D. length; I ++)// Traverse the HTML elements whose Tag Name Is Div
If (d [I]. classname = "Q2 ")// Obtain the HTML elements whose Tag Name Is Div and whose calss name is Q2
A = d [I]. getelementsbytagname ("");// Obtain the HTML elements whose Tag Name Is Div and whose Tag Name Is A in the HTML element whose calss name is Q2
For (I = 0; I <A. length; I ++)// Traverse the HTML elements whose tags are Div and whose tags are a in the HTML element whose calss is Q2
S + = A [I]. innerhtml;
Alert (s );
}
// -->
</SCRIPT>
--------------------------------------------------------------
There are many ways to solve a problem in HTML, CSS, and JS learning, and they have great flexibility. Therefore, it is necessary to have a systematic understanding of these methods, rather than one-sided understanding of a method, it is a good way to classify data.
- access node
- absolute access
- element
- set
- getelementbytag
- getelementbyname
- using class attributes
- relative access method
- parent-child/sibling relationship