Summary of how to get elements
1. Get the element based on the value of the id attribute and return it is an element object
document.getElementById("id属性的值"); document.getElementById("btn");
[Related course recommendations: JavaScript video tutorial]
2. Get the element based on the tag name and return It is a pseudo array that stores multiple DOM objects
document.getElementsByTagName("标签名字"); document.getElementsByTagName("li");
The following ones are not supported by some browsers
3 . Obtain elements based on the value of the name attribute, and return a pseudo array, which stores multiple DOM objects
document.getElementsByName("name属性的值") document.getElementsByName("name1")
4. According to the class style To get the element by name, what is returned is a pseudo array, which stores multiple DOM objects
document.getElementsByClassName("类样式的名字") document.getElementsByClassName("cls")
5. Get the element according to the selector and return is an element object
document.querySelector("选择器的名字"); document.querySelector("#btn"); //id,类,标签等选择器用的比较多
6. Get the element according to the selector, and what is returned is a pseudo array, which stores multiple DOM objects
document.querySelectorAll("选择器的名字")
This article comes from the js tutorial column, welcome to learn!
The above is the detailed content of Summary of ways to get DOM elements in js. For more information, please follow other related articles on the PHP Chinese website!