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

javascript getElementsByTagName_javascript技巧

WBOY
Release: 2016-05-16 18:11:08
Original
1106 people have browsed it
Copy code The code is as follows:

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.
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!