Selectors do not have a fixed definition. To a certain extent, jQuery's selectors are very similar to selectors in style sheets. The selector has the following characteristics:
1. Simplify code writing
2. Implicit iteration
3. There is no need to judge whether the object exists
where "$" is an indispensable part of the selector. In the jQuery library, $ is an abbreviation of jQuery, such as $("#foo") and jQuery("#foo") Equivalently, $.ajax and jQuery.ajax are equivalent. If there is no special instructions, you can understand the $ symbol in the program as the abbreviation of jQuery.
Now we officially enter the study of jQuery selector. Selectors are classified according to their functional habits. Different types of classifiers are classified below and explained respectively to enable readers to master them.
1. Basic selector
Basic selector includes 5 types of selectors: #id, element, .class, * and selectorl, selector2.selectorN. Each type will be introduced below with examples. The role of selectors and how to use them.
1. #id selector
#The id selector matches an element based on the given ID. If the selector contains special characters, they can be escaped with two slashes, and the return value is Array
2. element selector
The element selector is an element used for search. The tag name pointing to the DOM node. Its return value is Array
3. The class selector
. The class selector matches elements based on a given class and is a class to search for. An element can have multiple classes. As long as there is one match, it can be matched. The return value is Array
Example:
4. *Selector
* Selector is mostly used to search in context and match all elements. Its return value is Array
5. selector1, selector2, selectorN selector
This type of selector combines the elements matched by each selector and returns them together. You can specify any number of selectors and merge the matched elements into one result. The return value is: Array
2. Level selector
Level selector includes 5 forms: ancestor, descendant, parent > child, prev next and prev ~ siblings. The following uses examples to introduce the role and use of each selector in detail.
1. The ancestor descendant selector
refers to matching all descendant elements under a given ancestor element. The ancestor as a parameter represents any valid selector, while descendant is a selector used to match elements, and it is Descendants of the first selector. The return value is: Array
2. parent>child selector
parent>child selector means to match all child elements under a given parent element. The meanings of the two parameters are as follows: parent represents any valid selector; child is the selector used to match the element, and it is a child element of the first selector. Its return value is Array
3. prev next selector
This type of selector is used to match all next elements immediately following the prev element. The meanings of the two parameters are as follows: prev represents any valid selector; next represents a valid selector immediately following the first selector. Its return value is Array
4. prev ~ siblings selector
prev ~ siblings selector represents all siblings elements after matching the prev element. The meanings of the two parameters are as follows: prev represents any valid selector; siblings represents a selector and it serves as the sibling of the first selector. Its return value is Array
Example:
The code is as follows:
Example:
Query selector is summarized here. These are basically encountered in the learning process, and there are still a few parts that have not been summarized. After a period of practice, I believe everyone will be able to use the jQuery selector skillfully.