1. Overview When looking at the code, I found 1.1 Usage of querySelector() and querySelectorAll()querySelector() method Definition: Note: Syntax: Parameter value: String Required. Specifies one or more CSS selectors for matching elements. Select elements using their id, class, type, attribute, attribute value, etc. querySelectorAll() Method Definition: Tip: You can use the length property of the NodeList object to get the element attributes that match the selector, and then iterate over all elements to get the desired information; Syntax: Parameter value:
1.2 Usage of getElement(s)ByxxxxgetElementById() Method Definition: Syntax: Parameter value: getElementsByTagName() Method Definition: getElementsByClassName() method Definition: 2. Difference2.1 getElement(s)Byxxxx gets a dynamic collection, querySelector gets a static collectionDynamic means that the selected elements will change with the document, while static means that they will not be related to the changes in the document after they are taken out. Example 1: <body> <ul id="box"> <li class="a">Test 1</li> <li class="a">Test 2</li> <li class="a">Test 3</li> </ul> </body> <script type="text/javascript"> //Get ul, in order to dynamically add li later var ul = document.getElementById('box'); //Get the li in the existing ul var list = ul.getElementsByTagName('li'); for(var i =0; i < list.length; i++){ ul.appendChild(document.createElement('li')); //Dynamically append li } </script> The above code will fall into an infinite loop, i < list.length This loop condition. Example 1 Modification: Change the for loop condition to i < 4. As a result, 4 new elements are added to ul, so the number of li tags inserted now is 7. <body> <ul id="box"> <li class="a">Test 1</li> <li class="a">Test 2</li> <li class="a">Test 3</li> </ul> </body> <script type="text/javascript"> var ul = document.getElementById('box'); var list = ul.getElementsByTagName('li'); for(var i = 0; i < 4; i++){ ul.appendChild(document.createElement('li')); } console.log('list.length:',list.length); </script> Difference between Example 2: The following code static collection is reflected in <body> <ul id="box"> <li class="a">Test 1</li> <li class="a">Test 2</li> <li class="a">Test 3</li> </ul> </body> <script type="text/javascript"> var ul = document.querySelector('ul'); var list = ul.querySelectorAll('li'); for(var i = 0; i < list.length; i++){ ul.appendChild(document.createElement('li')); } console.log('list.length:',list.length); //The output result is still 3, not the number of li at this time 6 </script> The difference Why is it designed like this? The querySelectorAll method is clearly defined in the W3C specification: Let's take a look at what happens on Chrome: document.querySelectorAll('a').toString(); // return "[object NodeList]" document.getElementsByTagName('a').toString(); // return "[object HTMLCollection]" HTMLCollection is defined as follows in W3C:
In fact, This is a bit difficult to understand, it will be easier to understand if you look at the following example: var ul = document.getElementsByTagName('ul')[0], lis1 = ul.childNodes, lis2 = ul.children; console.log(lis1.toString(), lis1.length); // "[object NodeList]" 11 console.log(lis2.toString(), lis2.length); // "[object HTMLCollection]" 4
2.2 The received parameters are different The parameter received by var c1 = document.querySelectorAll('.b1 .c'); var c2 = document.getElementsByClassName('c'); var c3 = document.getElementsByClassName('b2')[0].getElementsByClassName('c'); Note: The parameters received by try { var e1 = document.getElementsByClassName('1a2b3c'); var e2 = document.querySelectorAll('.1a2b3c'); } catch (e) { console.error(e.message); } console.log(e1 && e1[0].className); console.log(e2 && e2[0].className); 2.3 Different browser compatibility
This is the end of this article about the difference between querySelector and getElementById methods in JavaScript. For more relevant content about querySelector and getElementById in JavaScript, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
>>: Pure CSS to achieve three-dimensional picture placement effect example code
Nginx log description Through access logs, you ca...
As you build and scale your Django applications, ...
Table of contents 1. Specify different packaging ...
Table of contents 1. Array.at() 2. Array.copyWith...
I heard that there is an interview question: How ...
Table of contents Component Infrastructure Purpos...
Table of contents Early creation method Factory P...
What is pip pip is a Python package management to...
The overall architecture of MySQL is divided into...
Below are some common parameters of /etc/my.cnf o...
For a website, usability refers to whether users c...
Detailed explanation of the misplacement of the in...
Seeing the recent popular WeChat tap function, I ...
CSS background: background:#00ffee; //Set the back...
When making a table page, sometimes the width set ...