Selectors are a very powerful feature of Later, DOM expanded the 1. querySelector queries a single element The syntax format is as follows: Document instance.querySelector(selector string); Element instance.querySelector(selector string); 1. Document instance call A simple example is as follows: // Get the body element let body = document.querySelector("body"); console.log(body) // Get the element with id container, only the first one will be obtained let container = document.querySelector("#container"); console.log(container) // Get the element containing btn in class, only the first one will be obtained let btn = document.querySelector(".btn"); console.log(btn); // Get the element containing btn in the direct subclass of container, only the first one will be obtained let containerBtn = document.querySelector("#container>.btn"); console.log(containerBtn); 2. Element instance call Simple example: // Get the element with ID container let container = document.querySelector("#container"); // Need to check whether the element object exists. Only if it exists, there is a querySelector method if (container) { // Only find elements whose class contains btn in the container. let containerBtn = container.querySelector(".btn"); console.log(containerBtn); } Theoretically, because CSS can obtain any element on the page through the selector, For example, the above example can be written directly as follows: let containerBtn = document.querySelector("#container .btn"); And because there is one less if judgment, the code is more concise. Of course, in some business scenarios, the 2. querySelectorAll queries all elements The Simple example: // Assume that the page has two div classes containing article // Get all elements whose class contains article let articleList = document.querySelectorAll(".article"); console.log(articleList); console.log(articleList.length); // Console output: // NodeList(2) [div.article, div.article] // 2 The // for of traversal for (let item of articleList) { console.log(item); } // for traversal for (let i = 0; i < articleList.length; i++) { console.log(articleList[i]); console.log(articleList.item(i)); } // forEach traverses articleList.forEach((item, index) => { console.log(item, index); }); 1. Problems with for in traversal If for 2. The problem of snapshots instead of real-time Consider the following example: //Use querySelectorAll to obtain, articleList is static, not real-time let articleList = document.querySelectorAll(".article"); console.log(articleList); console.log(articleList.length); // 2 setTimeout(() => { // Add an element let div = document.createElement("div"); div.className = "article"; document.body.appendChild(div); console.log(articleList); // Still 2 console.log(articleList.length); }, 0); Finally, a timer is set and a If Consider the following example: //Use getElementsByClassName to obtain, articleList is real-time let articleList = document.getElementsByClassName("article"); console.log(articleList); console.log(articleList.length); setTimeout(() => { // Add an element let div = document.createElement("div"); div.className = "article"; document.body.appendChild(div); console.log(articleList); // Here is 3 console.log(articleList.length); }, 0); View the print results in the console: HTMLCollection dynamic effects: The object obtained by using 3. Summary
This is the end of this article about You may also be interested in:
|
<<: Beginner's guide to building a website ⑦: It's so easy to make a beautiful website
>>: Example code for using HTML ul and li tags to display images
1 Introduction Apache Storm is a free, open sourc...
XHTML is the standard website design language cur...
Scenario: As the amount of data increases, the di...
This article shares the specific code of JavaScri...
introduce Usually a background server program mus...
I was bored and suddenly thought of the implementa...
This article mainly introduces how to build a MyS...
Table of contents 1. v-on directive 1. Basic usag...
Quickly install the tensorflow environment in Doc...
If you want to install some 64-bit applications (...
Preface The reason why MySQL's innodb engine ...
Table of contents 1. Scenario description: 2. Cas...
I will use three days to complete the static page...
Relative Length Units em Description: Relative len...
GitHub address, you can star it if you like it Pl...