The javascript advanced selector querySelector and querySelectorAll are fully parsed. The selector

Source: Internet
Author: User

The javascript advanced selector querySelector and querySelectorAll are fully parsed. The selector

The querySelector and querySelectorAll methods are defined in the W3C Selectors API specification. Their role is to easily locate specified elements in the document according to the CSS selector specification.

Almost all mainstream browsers currently support them. Includes IE8 or later versions, Firefox, Chrome, Safari, and 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; };

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

Get the elements whose page I property D is test:

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

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

document.getElementsByClassName('red')document.querySelector('.red')document.querySelectorAll('.red')

Ps:

Note that the elements in the returned nodeList set are non-real-time (no-live). To distinguish between real-time and non-real-time returned results, see the following example:

 <div id="container">   <div></div>   <div></div> </div>
// First select the element container = document whose id is container on the page. getElementById ('# iner'); console. log (container. childNodes. length) // The result is 2 // then add a sub-element container to it by code. appendChild (document. createElement ('div '); // This element is not only added to the page, but the variable container automatically updates the console. log (container. childNodes. length) // The result is 3.

Through the above example, we can understand what elements are updated in real time. Document. getElementById returns the real-time result. After adding a child element to it, you can obtain the number of all child elements again, it has been updated from the original two to three (Some browsers, such as Chrome, will resolve the blank space as a subnode ).

Differences between Element. querySelector and Element. querySelectorAll and jQuery (element). find (selector) selectors:

 

<SPAN style = "FONT-SIZE: 15px "> <divid =" test1 "> <ahref =" http://www.bkjia.com/"> helping customers </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.bkjia.com/"> help house </a> console. log (obj2.length) // 1 console. log (obj3) // null </script> </SPAN>

QuerySelectorAllFind all nodes that comply with the selector description in the document, including the Element itself.

JQuery (element). find (selector)Find all nodes in the document that conform to the selector description, excluding the Element itself.

The above javascript advanced selector querySelector and querySelectorAll are all the content shared by Alibaba Cloud. I hope you can give us a reference and support for our guests.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.