JQuery selector User Guide, jqueryselector

Source: Internet
Author: User

JQuery selector User Guide, jqueryselector

Introduction

HTML5 introduces the document. querySelector and document. querySelectorAll methods to the Web API to conveniently select elements from the DOM. The function is similar to the jQuery selector. This makes it much easier to write native JavaScript code.
Usage

The two methods use similar syntaxes and both receive a string parameter. This parameter must be a valid CSS selection syntax.

Copy codeThe Code is as follows:
Element = document. querySelector ('selectors ');
ElementList = document. querySelectorAll ('selectors ');

The selectors parameter can contain multiple CSS selectors separated by commas.

Copy codeThe Code is as follows:
Element = document. querySelector ('selector1, selector2 ,...');
ElementList = document. querySelectorAll ('selector1, selector2 ,...');

The two methods cannot be used to find elements with pseudo-class States. For example, querySelector (': hover') does not get the expected results.

QuerySelector

Copy codeThe Code is as follows:
Element = document. querySelector ('div # iner '); // return the first div whose id is container.
Element = document. querySelector ('. foo,. bar'); // return the first element with the foo or bar style class.

QuerySelectorAll

This method returns all elements that meet the conditions. The result is a nodeList set. The Search rules are the same as described above.

Elements = document. querySelectorAll ('div. foo'); // return all div with foo class style
Note that the elements in the returned nodeList set are non-real-time.

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.