JS gets elements using getElementById (), Getelementsbyname (), getElementsByTagName () usage and differences

Source: Internet
Author: User
Tags tag name tagname

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.