function getElementsByTagName(node, tagName) {
var elements = [ ], i = 0, anyTag = tagName === "*", next = node.firstChild;
while ((node = next)) {
if (anyTag ? node.nodeType === 1 : node .nodeName === tagName) elements[i ] = node;
next = node.firstChild || node.nextSibling;
while (!next && (node = node.parentNode)) next = node.nextSibling;
}
return elements; gather.
Syntax
document.getElementsByTagName(tagname)
Description
The getElementsByTagName() method returns elements in the order they are in the document.
If you pass the special string "*" to the getElementsByTagName() method, it will return a list of all elements in the document, and the order in which the elements are arranged is the order they are in the document.
Tips and Notes Note: The string passed to the getElementsByTagName() method can be case-insensitive.
Example
Example 1
Copy code
The code is as follows:
Copy the code
The code is as follows:
If you know the document very well structure, you can also use the getElementsByTagName() method to get a specific element in the document. For example, the following code can get the fourth paragraph in the document:
var myParagragh = document.getElementsByTagName("p")[3];
However, we still think that if you need to operate on a specific element, it is more efficient to use the getElementById() method.