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
The database, like the operating system, is a sha...
Table of contents 1. Scene loading 2. Find Node 1...
Table of contents Background 1. What is dns-prefe...
Before reading this article, it is best to have a...
Because I need to install MySQL, I record the ins...
Apache Log4j2 reported a nuclear-level vulnerabil...
After reading the following article, you can depl...
This article is based on the Windows 10 system en...
This article shares the specific code for drawing...
1. Introduction Some time ago, there were a serie...
You can often see articles about CSS drawing, suc...
Preface Under the influence of some CSS interacti...
Table of contents Business requirements: Solution...
Environment Introduction Operating system: centos...
Table of contents About FastDFS 1. Search for ima...