1. getElementById()
getElementById() can access a specific element in DOCUMENT. As the name suggests, it obtains the element through the ID, so it can only access the element with the ID set.
For example, there is a DIV with the ID docid:
program code
<div id=docid></div>
Then you can use getElementById(docid) to get this element.
2.getElementsByName()
This is to get elements through NAME, but I wonder if you have noticed that this is GET ELEMENTS. The plural ELEMENTS means that what is obtained is not an element. Why?
Because the ID of each element in DOCUMENT is unique, but the NAME can be repeated. To use an analogy, it's 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
If there are more than two tags with the same name, then getElementsByName() can obtain these elements to form an array.
For example, there are two DIVs:
program code
<div name=docname id=docid1></div>
<div name=docname id=docid2></div>
Then you can use getElementsByName(docname) to obtain these two DIVs, use getElementsByName(docname)[0] to access the first DIV, and use getElementsByName(docname)[1] to access the second DIV.
3. getElementsByTagName()
This is to obtain elements through TAGNAME (tag name). Of course, a DOCUMENT will have the same tag, so this method also obtains an array.
The following example has two DIVs. You can access them with getElementsByTagName(div). Use getElementsByTagName(div)[0] to access the first DIV. Use
getElementsByTagName(div)[1] accesses the second DIV.
To summarize the standard DOM, try to use the standard getElementById() to access a specific element, and use the standard getElementByTagName() to access tags. However, IE does not support getElementsByName(), so you should avoid using it.
getElementsByName(), but getElementsByName() and the non-standard document.all[] are not useless. They have their own conveniences. Whether you use them or not depends on what browser the website users use. It is up to you. .