Document. all and getElementById, getElementsByName, getElementsByT

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.
Copy codeThe 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:
<Html> <pead> <title> document. all test </title> </pead> <body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
At this time, the values I entered in the two texts are eee and www.
Test results in IE: eee, eee
When I remove the id in <input type = "text" name = "aaa" id = "ccc">, that is, when the above Code uses the content in the comment
I also enter eee, www
Test results in IE: www, eee
Carefully analyze the results of the above two tests: the conclusion is that every time getElementById is executed, all the forms on the web page are traversed in order, and the values of id and name are searched at the same time, if the id you want to find exists, you will not continue searching. If there is no corresponding id corresponding to it, you can check whether the name value corresponds to it. If there is a corresponding name corresponding to it, then, the search will not continue. That is to say:
When ie executes document. getElementById (elementName), the first object with name or id equal to elementName is returned, not simply by ID.
The first result of the same two tests in firefox is eee, www, and the second result returns null because no id = "ccc" is found.
For this result, see
Http://www.jb51.net/article/16852.htm
He also analyzed getElementById and getElementByName, which is worth noting.

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.