Document. All and getelementbyid, getelementsbyname, and getelementsbytagname usage difference-getelementbyid

Source: Internet
Author: User

However, if you need to find a specific element in the document, the most effective method is getelementbyid ().
However, when using getelementbyid, The results may be different for different browsers. CopyCode The Code is as follows: text1:
<Input name = "textname1" type = "text" id = "textname2"/>
<Br>
Text2:
<Input name = "textname2" type = "text" id = "textname1"/>
<Br>
<Input type = "button" name = "Submit" value = "text1" onclick = alert (document. getelementbyid ('textame2'). Value)/>
<Input type = "button" name = "submit2" value = "text2" onclick = alert (document. getelementbyid ('textame1'). Value)/>
I tested the above Code in IE, input 1 in the first text box, input 2 in the second text box, and click the two buttons below to guess what the result is?
In my intention, button 1 returns the value of the first text box, And button 2 returns the value of the text box.
The result is that both buttons return the value of the first text box.
This indicates that when ie executes document. getelementbyid (elementname), the first object with name or ID equal to elementname is returned, which is not searched by ID.
This problem does not exist in Firefox. When Firefox executes document. getelementbyid (elementname), it only finds the object whose ID is equal to elementname. If it does not exist, null is returned.
It is possible that IE is concerned with compatibility issues.

The following code is used for testing:<Title> <br/> document. All test <br/> </title> <br/> <p>

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.