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
Chatbots can save a lot of manual work and can be...
1. What is ElasticSearch? Elasticsearch is also d...
In general guestbooks, forums and other places, t...
1. First create the file (cd to the directory whe...
In the official MySQL dump tool, how can I restor...
The following code introduces the installation me...
I reinstalled the system some time ago, but I did...
Table of contents 1. Cartesian product phenomenon...
Table of contents The beginning of the story Inst...
This article shares the specific code of React to...
mysql 5.7.21 winx64 installation and configuratio...
Arrow function is a new feature in ES6. It does n...
In our daily work, we often come into contact wit...
Preface What is data type conversion? The default...
Table of contents Preface 1. Usage examples 2. Im...