Difference between getelementbyid () getelementsbyname () getelementsbytagname ()

Source: Internet
Author: User
Web Standard Getelementbyid (), getelementsbyname (), and getelementsbytagname () Access Billing NENT Any tag in:

1. getelementbyid ("ID ") Getelementbyid () Accessible Billing NENT A specific element in ID So you can only access ID . For example, there is Div Of ID Is Docid : <Div id = "docid"> </div> Then you can use Getelementbyid ("docid ") To obtain this element.

2. getelementsbyname ("name ") This is through Name To obtain the element, but I do not know whether it is. This is Get elemnets , Plural Elemnets It is not an element. Why? Because Billing NENT For each element ID Is unique, Name But it 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 a document contains more than two tags Name Same, then Getelementsbyname () These elements can be obtained to form an array. For example, there are two Div : <Div name = "docname" id = "docid1"> </div> <Div name = "docname" id = "docid2"> </div> You can use Getelementsbyname ("docname ") Obtain the two Div , Use Getelementsbyname ("docname") [0] Access the first Div , Use Getelementsbyname ("docname") [1] Access the second Div . 3. getelementsbytagname ("tagname ") This is through Tagname (Tag Name) to obtain the element, Document Of course there will be the same tag, so this method also getsArray. In the following example, there are two Div , You can use Getelementsbytagname ("Div ") To access them, use Getelementsbytagname ("Div") [0] Access the first Div, , Use Getelementsbytagname ("Div") [1] 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 >
</ 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 >

 

Total Conclusion: Summary Standard Dom To access a specific element and try to use the standard Getelementbyid () , Use standard Getelementbytagname (), However IE Not Supported Getelementsbyname () , Avoid using Getelementsbyname () , Getelementsbyname () And non-conforming Document. All [] It 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. Javascript In Getelementbyid But in standard pages, ID It can only appear once. If I want to control multiple elements at the same time, such as clicking a link, How to hide multiple layers? Use Class . Source: http://www.cnblogs.com/zpq521/archive/2008/07/12/1241492.html

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.