Home > Web Front-end > JS Tutorial > Summary of ways to get DOM elements in js

Summary of ways to get DOM elements in js

angryTom
Release: 2019-12-11 17:28:11
forward
3033 people have browsed it

Summary of ways to get DOM elements in js

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");
Copy after login

[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");
Copy after login

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")
Copy after login

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")
Copy after login

5. Get the element according to the selector and return is an element object

document.querySelector("选择器的名字");
document.querySelector("#btn"); //id,类,标签等选择器用的比较多
Copy after login

6. Get the element according to the selector, and what is returned is a pseudo array, which stores multiple DOM objects

document.querySelectorAll("选择器的名字")
Copy after login

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!

Related labels:
js
source:cnblogs.com
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