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

javascript method to get elements

藏色散人
Release: 2023-01-05 16:14:11
Original
5601 people have browsed it

Javascript method of obtaining elements: 1. Get the node object with the id attribute through getElementById; 2. Get the object array through getElementsByTagName; 3. Get the element with the specified class name through getElementsByClassName.

javascript method to get elements

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

There are three common ways to obtain elements, namely through element ID, through tag name and through class name.

getElementById

DOM provides a method called getElementById, which will return a node object corresponding to the id attribute. Please pay attention to case sensitivity when using it.

It is a function unique to the document object, and this method can only be called through it. The method used is as follows:

The code is as follows:

document.getElementById('demo') //demo是元素对应的ID
Copy after login

This method is compatible with mainstream browsers, even IE6, and can be used boldly.

getElementsByTagName

This method returns an array of objects (HTMLCollection collection to be precise, it is not an array in the true sense). Each object corresponds to the object in the document. An element with the given tag. Similar to getElementById, this method only provides one parameter, and its parameter is the name of the specified tag. The sample code is as follows:

The code is as follows:

document.getElementsByTagname('li')  //li是标签的名字
Copy after login

It should be noted that this method has In addition to being called by document objects, it can also be called by ordinary elements. The example is as follows:

The code is as follows:

var demo = document.getElementById('demo');
var lis = demo.getElementsByTagname('li');
Copy after login

Similarly, this method is compatible with mainstream browsers, even IE6, and can be used boldly.

getElementsByClassName

In addition to obtaining elements by specifying tags, DOM also provides the getElementsByClassName method to obtain elements with specified class names. However, because this method is relatively new, older browsers do not yet support it, such as IE6. However, we can use hacks to make up for the shortcomings of old browsers. The method of calling this method is as follows:

The code is as follows:

document.getElementsByClassName('demo')    //demo为元素指定的class名
Copy after login

Same as getElementsByTagname, in addition to being called by the document object, this method can also be called by ordinary elements. .

For older browsers, such as IE6 and 7, we can implement it through the following hack method:

The code is as follows:

function getElementsByClassName(node,classname){
        if(node.getElementsByClassName) {
            return node.getElementsByClassName(classname);
        }else {
            var results = [];
            var elems = node.getElementsByTagName("*");
            for(var i = 0; i < elems.length; i++){
                if(elems[i].className.indexOf(classname) != -1){
                    results[results.length] = elems[i];
                }
            }
            return results;
        }
    }
Copy after login

Extension

If you are not only satisfied with the above element selection methods, but also want to obtain elements through selectors like JQuery. The implementation method is similar to the getElementsByClassName above. If you are interested, you can implement a set of selectors yourself. However, I think the above three methods combined with event bubbling are enough. After all, these three methods are considered excellent in terms of performance.

Recommended study: "javascript advanced tutorial"

The above is the detailed content of javascript method to get elements. For more information, please follow other related articles on the PHP Chinese website!

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 [email protected]
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!