Web standards can access any of the tags in Documnent through getElementById (), Getelementsbyname (), and getElementsByTagName (): 1 getElementById ("ID ") getElementById () can access a specific element in the Documnent, as the name implies, by using the ID to get the element, so only the element with the ID set is accessible. For example, there is a div with an ID of docid:
<div id= "DocId" ></div>
Then you can use getElementById ("DocId") to get this element.
2 Getelementsbyname ("name") this is through the Name to get the element, but I do not know that there is no, this is get elemnets, the plural elemnets represents not an element, why? Because the ID of each element in Documnent is unique, name can be duplicated. A metaphor is like a person's ID number is unique (theoretically, although there is a repetition in reality), but the name repeats a lot. If a document has more than two tags in the same name, then Getelementsbyname () can take these elements to make up an array. For example, there are two div:
<div name= "DocName" id= "docid1" ></div><div name= "DocName" id= "Docid2" ></div>
Then you can use Getelementsbyname ("DocName") to obtain the two Div, with Getelementsbyname ("DocName") [0] to access the first Div, with Getelementsbyname ("DocName ") [1] to access the second Div. 3 getElementsByTagName ("TagName") This is done by TagName (tag name) to get the element, a document will of course have the same label, so this method is also to get an array. The following example has two Div, which can be accessed using getElementsByTagName ("div"), using getElementsByTagName ("div") [0] to access the first Div, Use getElementsByTagName ("div") [1] to access the second Div.
#docid1, #docid2 {margin:10px; height:400px; width:400px; } --></style>function bgcolor () { var docnobj= document.getElementsByTagName ("div" 0].style.backgroundcolor = "Black" ; docnobj[ 1].style.backgroundcolor = "Black" ;} --></script>
Summary: Summarize the standard DOM, access a specific element as far as possible with the standard getElementById (), access the label with the standard Getelementbytagname (), but IE does not support getelementsbyname (), So avoid using getelementsbyname (), but Getelementsbyname () and non-conforming document.all[] are not all virgin, they have their own convenience, with no need to look at the site users use what browser, It's up to you to decide.
JS gets elements using getElementById (), Getelementsbyname (), getElementsByTagName () usage and differences