Difference between getelementbyid () getelementsbyname () getelementsbytagname ()

Source: Internet
Author: User
Tags tagname
Getelementbyid () getelementsbyname () getelementsbytagname () DifferenceIn the Web standard, you can use getelementbyid (), getelementsbyname (), and getelementsbytagname () to access any tag in javasnent: 1 getelementbyid ("ID ")Getelementbyid () can access a specific element in intent NENT. As the name suggests, it is to get the element through ID, so it can only access the element with the ID set. For example, if the ID of a div is docid: <Div id = "docid"> </div>, you can use getelementbyid ("docid") to obtain this element.

2 getelementsbyname ("name ")This is an element obtained by name, but I do not know whether it is. This is get elemnets. The Plural elemnets indicates that it is not an element. Why? Because the ID of each element in comment NENT 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 there are many duplicate names. 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 ") get the two divs, use getelementsbyname ("docname") [0] to access the first Div, and use getelementsbyname ("docname") [1] to access the second Div.3 getelementsbytagname ("tagname ")This is throughTagname(Tag Name) to obtain elements. Of course, a document has 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 ,, use getelementsbytagname ("Div") [1] to access the second Div. <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>

TotalConclusion:SummaryUse standard DOM to access a specific element. Use standard getelementbyid () and Standard getelementbytagname () for access tags (),HoweverIENot SupportedGetelementsbyname ()Therefore, you must avoid using getelementsbyname (), but getelementsbyname () and does not comply with the standard document. all [] is not completely useless. They have their own convenience. If you don't need to use them, you can 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? Use class.

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.