Getelementbyid getelementsbyname getelementsbytagname
Knowledge needs to be updated and new, and those you have read will also be forgotten. You need to consolidate it. ^_^
In the Web standard, you can use getelementbyid (), getelementsbyname (), and getelementsbytagname () to access any tag in the document:
1. getelementbyid ()
Getelementbyid () can access a specific element in the document. As the name suggests, the element is obtained by ID. Therefore, only the element with the ID set can be accessed.
For example, the ID of a div is docid:
<Div id = "docid"> </div>
You can use getelementbyid ("docid") to obtain this element.
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"
Http://www.w3.org/TR/html4/loose.dtd ">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> byid </title>
<Style type = "text/CSS">
<! --
# Docid {
Height: 400px;
Width: 400px;
Background-color: #999 ;}
-->
</Style>
</Head>
<Body> <Div id = "docid" name = "docname" onclick = "bgcolor ()"> </div>
</Body>
</Html>
<Script language = "JavaScript" type = "text/JavaScript">
<! --
Function bgcolor (){
Document. getelementbyid ("docid"). style. backgroundcolor = "#000"
}
-->
</SCRIPT>
, Getelementsbyname ()
This is an element obtained by name, but I do not know whether it is. This is get elements. The Plural elements indicates that it is not an element. Why?
The ID of each element in the document is unique, but the name can be repeated. A metaphor is like a person's ID card number is unique (theoretically, although there are duplicates in reality), but the name
There are many duplicates. If more than two tags have the same name in a document, getelementsbyname () can obtain these elements to form an array.
For example, there are two divs:
<Div name = "docname" id = "docid1"> </div>
<Div name = "docname" id = "docid2"> </div>
You can use getelementsbyname ("docname") to obtain the two divs, use getelementsbyname ("docname") [0] to access the first Divs, and use getelementsbyname
3. getelementsbytagname ()
In this case, the tagname is used to obtain the element. Of course, a document contains the same tag, so this method also gets an array.
In the following example, there are two divs. You can use getelementsbytagname ("Div") to access them and use getelementsbytagname ("Div") [0] to access the first Div.
Getelementsbytagname ("Div") [1] access the second Div.
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<HTML xmlns = "http://www.w3.org/1999/xhtml ">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> byname, tag </title>
<Style type = "text/CSS">
<! --
# Docid1, # docid2 {
Margin: 10px;
Height: 400px;
Width: 400px;
Background-color: #999 ;}
-->
</Style>
</Head>
<Body>
<Div name = "docname" id = "docid1" onclick = "bgcolor ()"> </div>
<Div name = "docname" id = "docid2" onclick = "bgcolor ()"> </div>
</Body>
</Html>
<Script language = "JavaScript" type = "text/JavaScript">
<! --
Function bgcolor (){
VaR docnobj = Document. getelementsbytagname ("Div ");
Docnobj [0]. style. backgroundcolor = "black ";
Docnobj [1]. style. backgroundcolor = "black ";
}
-->
</SCRIPT>
To sum up the standard DOM, try to use the standard getelementbyid () for accessing a specific element, and use the standard getelementbytagname () for access tags, but IE does not support
Getelementsbyname (), so it is necessary to avoid using getelementsbyname (), but getelementsbyname () and non-conforming document. All [] are not completely useless, it
They have their own convenience. If you don't need to use it, it's up to you to see what browsers the website users use.
Getelementbyid in Javascript is very commonly used, but one ID can only appear once on a standard page. If I want to control multiple elements at the same time, such as clicking a link to hide multiple layers, what should I do? If you use a class, of course, the same class can be repeated on the page. Is there any getelementbyclass? No, but the solution is as follows:
// Create an array
VaR allpagetags = new array ();
Function hidedivwithclasses (theclass ){
// Populate the array with all the page tags
VaR allpagetags = Document. getelementsbytagname ("Div ");
// Cycle through the tags using a For Loop
For (I = 0; I // pick out the tags with our class name
If (allpagetags . Classname = theclass ){
// Manipulate this in whatever way you want
Allpagetags. Style. Display = 'none ';
}
}
}