Home > Web Front-end > JS Tutorial > Comprehensive analysis of javascript advanced selectors querySelector and querySelectorAll_Basic knowledge

Comprehensive analysis of javascript advanced selectors querySelector and querySelectorAll_Basic knowledge

WBOY
Release: 2016-05-16 15:06:11
Original
1688 people have browsed it

querySelector and querySelectorAll methods are defined in the W3C Selectors API specification. Their function is to conveniently locate specified elements in the document according to CSS selector specifications.

At present, almost all major browsers support them. Including IE8 (inclusive) and above, Firefox, Chrome, Safari, Opera.

querySelector and querySelectorAll define the following interfaces in the specification:

module dom { [Supplemental, NoInterfaceObject] interface NodeSelector { Element querySelector(in DOMString selectors); NodeList querySelectorAll(in DOMString selectors); }; Document implements NodeSelector; DocumentFragment implements NodeSelector; Element implements NodeSelector; };
Copy after login

From the interface definition, you can see that Document, DocumentFragment, and Element all implement the NodeSelector interface. That is, these three types of elements all have two methods. The parameters of querySelector and querySelectorAll must be strings that conform to css selector. The difference is that querySelector returns an object, and querySelectorAll returns a collection (NodeList).

Get the element whose I attribute D is test on the page:

1 document.getElementById("test");
2 document.querySelector("#test");
3 document.querySelectorAll("#test")[0];
Copy after login

Get the elements whose class attribute is "red" on the page:

document.getElementsByClassName('red')
document.querySelector('.red')
document.querySelectorAll('.red')
Copy after login

ps:

But it should be noted that the elements in the returned nodeList collection are non-live. If you want to distinguish between real-time and non-real-time return results, please see the following example:

 <div id="container">
   <div></div>
   <div></div>
 </div>
Copy after login
//首先选取页面中id为container的元素
container=document.getElementById('#container');
console.log(container.childNodes.length)//结果为2
//然后通过代码为其添加一个子元素
container.appendChild(document.createElement('div'));
//这个元素不但添加到页面了,这里的变量container也自动更新了
console.log(container.childNodes.length)//结果为3
Copy after login

Through the above example, you can have a good understanding of what elements are that update in real time. document.getElementById returns the real-time result. After adding a sub-element to it, the number of all sub-elements is obtained again, which has been updated from the original 2 to 3 (this does not take into account that some browsers such as Chrome will blank also resolves to a child node).

The difference between Element.querySelector and Element.querySelectorAll and jQuery(element).find(selector) selector:

<SPAN style="FONT-SIZE: 15px"><divid="test1"><ahref="http://www.jb51.net/">脚本之家</a></div> 
<pid="bar">111</p> 
<script> 
var d1 = document.getElementById('test1'), 
obj1 = d1.querySelector('div a'), 
obj2 = d1.querySelectorAll('div a'); 
obj3 = $(d1).find('div a'); 
console.log(obj1)//<a href="http://www.jb51.net/">脚本之家</a> 
console.log(obj2.length)//1 
console.log(obj3)//null 
</script> 
</SPAN> 
Copy after login

querySelectorAll Find all nodes in the document that match the selector description, including the Element itself

jQuery(element).find(selector) Find all nodes in the document that match the selector description, excluding the Element itself

The above comprehensive analysis of the javascript advanced selectors querySelector and querySelectorAll is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Script Home.

Related labels:
source:php.cn
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