Home  >  Article  >  Web Front-end  >  Summary of the usage of querySelector selector in jQuery

Summary of the usage of querySelector selector in jQuery

巴扎黑
巴扎黑Original
2017-06-21 09:49:442689browse

This article mainly introduces the relevant information about the usage guide of jQuery selector querySelector. Friends who need it can refer to it

Introduction

HTML5 is new to Web API Two methods, document.querySelector and document.querySelectorAll, are introduced to more conveniently select elements from the DOM. Their functions are similar to jQuery's selectors. This makes it much easier when writing native JavaScript code.
Usage

The two methods use similar syntax, and both receive a string parameter. This parameter needs to be a legal CSS selection syntax.

The code is as follows:

element = document.querySelector('selectors');
elementList = document.querySelectorAll('selectors');

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

The code is as follows:

element = document.querySelector('selector1,selector2,...');
elementList = document.querySelectorAll('selector1,selector2,...');

Using these two methods cannot find elements with pseudo-class status, such as querySelector(':hover') Will not get expected results.

querySelector

The code is as follows:

element = document.querySelector('p#container');//返回id为container的首个p
element = document.querySelector('.foo,.bar');//返回带有foo或者bar样式类的首个元素

querySelectorAll

This method returns all elements that meet the conditions , the result is a nodeList collection. The search rules are the same as described before.

elements = document.querySelectorAll('p.foo');//Return all p with foo class style
It should be noted that the elements in the returned nodeList collection are not real-time.

The above is the detailed content of Summary of the usage of querySelector selector in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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