Home > Web Front-end > JS Tutorial > body text

Differences in usage between document.all and getElementById, getElementsByName, getElementsByTagName - getElementById_javascript skills

WBOY
Release: 2016-05-16 18:57:35
Original
1319 people have browsed it

However, if you need to find a specific element in the document, the most efficient method is getElementById().
However, it should be noted that the results of using getElementById may be different for different browsers. The following are relevant instructions

Copy code The code is as follows:

text1:



text2:





I tested the above code in IE, enter 1 in the first text box, enter 2 in the second text box, and then click below Two buttons, guess what the result is?
My original intention is that button 1 returns the value of the first text box, and button 2 returns the value of the first text box.
The result is that both buttons return the value of the first text box.
Explain that when ie executes document.getElementById(elementName), it returns the first object whose name or id is equal to elementName, and is not searched based on ID.
There is no such problem in fireFox. When fireFox executes document.getElementById(elementName), it only looks for objects with an id equal to elementName. If it does not exist, it returns null.
Maybe IE did this because of compatibility issues.

Here is the code I used for testing:

[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

At this time, I am in two The values ​​entered in text are: eee, www
The test results in IE are: eee, eee
Look again when I put I also enter eee, www
The test results in IE are: www,eee
Carefully analyze the results of the above two tests: The conclusion is: every time getElementById is executed, all the forms in the web page will be traversed in order, and the two values ​​​​of id and name will be searched at the same time. If the id you are looking for exists, the search will not continue. If there is no corresponding id and The correspondence depends on whether the name value corresponds to it. If there is a corresponding name corresponding to it, the search will not continue. That is to say:
When ie executes document.getElementById(elementName), what is returned is the first object whose name or id is equal to elementName, and is not searched only by ID.
The first result of the same two tests under firefox is eee, www, while the second result returns null because the id="ccc" is not found
For this result, please also see http://www.jb51.net/article/16852.htm
He also analyzed getElementById and getElementByName, which is worth a look.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!