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>