What are the nine selectors in JQ? What is the function?

2021-10-14 11:36作者:admin来源:未知>次阅读

Role of JQ nine selectors

the nine selectors are used to find element nodes. JQuery gives me nine types of selectors.

1. Basic selector

the basic selector is the most commonly used and simplest selector in jQuery. It finds DOM elements through element ID, class and tag name (ID can only be used once in web pages, and class can be reused). The so-called ID in a web page can only be used once means that the ID in a web page cannot be repeated, that is, it is unique.

1、
Usage: $(% 26#34; #mydiv% 26#34;); Returns a collection of values consisting of a single element
Note: This is to directly select id =% 26#34 in HTML; myDiv"
2、
Usage: $(% 26#34; div% 26#34;) return value collection element
Note: the English translation of element is \
Input, a, etc
3、
Usage: $(% 26#34;. MyClass% 26#34;) return value collection element
Note: this tab directly selects class =% 26#34 in HTML code; myClass" Element or group of elements (because in the same HTML page)
Class can have multiple same values)
4、
Usage: $(% 26#34; *% 26#34;) return value collection element
Description: matches all elements. It is mostly used to search in combination with context
5、
Usage: $(% 26#34; div, #id,. MyClass, p.myclass, span#id% 26#34;) return value collection element
Description: merge the elements matched by each selector and return them together. You can specify any number of selectors and merge the matched elements into one result.
Where p.myclass is the matching element
p class=" myClass" 
2. The hierarchy selector

can perform chain operation only if the jQuery object returned by this method. If you want to obtain specific elements through the hierarchical relationship between DOM elements, such as descendant elements, child elements, adjacent elements, sibling elements, etc., you need to use a hierarchical selector.

1 、
Usage: $(% 26#34; form input% 26#34;); Return value collection element
Description: match all descendant elements under a given ancestor element. This should be the same as" arent >  child" Distinguish.
Look in the ancestor element, including child elements and child elements of child elements.
2、
Usage: $(% 26#34; form% 26gt; input% 26#34;); Return value collection element
Description: matches all child elements under the given parent element. Note: distinguish the descendant element from the child element
Find in the parent element, only child elements are included, and child elements without child elements are included.
3、
Usage: $(" label + input% 26#34;); Return value collection element
Description: matches all the next elements immediately after the prev element
4、
Usage: $(% 26#34; form ~ input% 26#34;); Return value collection element
Note: match all siblings after prev element. Note: it is the element after matching and does not include this element. Moreover, the siblings method of jQuery matches the elements of the same generation as prev, and its subsequent elements are not matched
Note: (% 26#34; prev ~ div% 26#34;) selector can only select% 26#34# prev" Peer element after element; The method siblings () in jQuery has nothing to do with the front and back positions, and can be selected as long as it is a peer node
3. The filter selector

finds a bunch of page elements. We can add filter conditions to these elements, find the elements we want, and then filter them. Filter out the required DOM elements through specific filtering rules, and the selector starts with \ Usage: $(% 26#34; tr: first% 26#34;); Returns a collection of values consisting of a single element Description: matches the first element found 2、 Usage: $(% 26#34; tr: last% 26#34;) return value collection element Description: matches the last element found. Corresponds to: first 3、 Usage: $(" input: not (: checked)% 26#34;) return value collection element Note: remove all elements matching the given selector. It is somewhat similar to \ 4、 Usage: $(% 26#34; tr: even% 26#34;) return value collection element Note: match all elements with even index values, and count from 0. JS arrays are counted from 0. For example, if you want to select rows in table, because the counting starts from 0, the first TR in table is even 0 5、 Usage: $(% 26#34; tr: odd% 26#34;) return value collection element Description: matches all elements with odd index values, corresponds to: even, and counts from 0 6、 Usage: $(% 26#34; tr: EQ (0)% 26#34;) return value collection element Description: the element matching a given index value. EQ (0) is to get the first tr element. The index value in parentheses is not the element permutation number 7、 Usage: $(% 26#34; tr: GT (0)% 26#34;) return value collection element Description: matches all elements greater than the given index value 8、 Usage: $(% 26#34; tr: LT (2)% 26#34;) return value collection element Description: matches all elements less than the given index value 9. : header (fixed writing) Usage: $(% 26#34;: header% 26#34;). CSS (% 26#34; background% 26#34;,% 26#34; #eee% 26#34;) returns a value collection element Description: match Title elements such as H1, H2 and H3. This is specially used to obtain title elements such as H1 and H2 10. : animated (fixed writing) return value collection element Description: match all elements

< 2> Content filter selector

the filtering rules of the content filter selector are mainly reflected in its sub elements and text content.

1、
Usage: $(% 26#34; div: contains (% 26#39; John% 26#39;)% 26#34;) returns a value collection element
Description: match the element containing the given text. This selector is useful. It comes in handy when we want to select a non DOM tag element,
Its function is to find whether the text content enclosed by the label conforms to the specified content.
2、
Usage: $(% 26#34; TD: empty% 26#34;) return value collection element
Description: matches all empty elements that do not contain child elements or text.
3、
Usage: $(% 26#34; div: has (% 26#39;. Mini% 26#39;)% 26#34;) returns a value collection element
Description: matches the element that contains the element that the selector matches.
4、
Usage: $(% 26#34; TD: parent% 26#34;) return value collection element
Description: matches the elements with child elements or text. Note: here is": \
Usage: $(\
Note: match all invisible elements. If the type attribute of the input element is \
It means display: none and input type =% 26#34; hidden" All will be matched. Similarly, completely distinguish the colon":% in your mind 26#34;,  Point number".% 26#34; And comma",% 26#34; The difference.
2、
Usage: $(% 26#34; tr: visible% 26#34;) return value collection element
Description: matches all visible elements 

< 4> Attribute filter selector

the filter rule of the attribute filter selector is to obtain the corresponding element through the attribute of the element.

1、
Usage: $(% 26#34; div [ID]% 26#34;); Return value collection element
Description: matches the element containing the given attribute. In the example, all div tags with \
2、
Usage: $(% 26#34; input [name =% 26#39; Newsletter% 26#39;]% 26#34;). Attr (% 26#34; checked% 26#34;, true); Return value collection element
Note: the matching given attribute is an element with a specific value. In the example, all input elements whose name attribute is newsletter are selected.
3、
$("div[title!='test']").css("background","yellow");
Usage: $(\
Description: matches all elements that do not contain the specified attribute or whose attribute is not equal to a specific value.
This selector is equivalent to: not this selector is equivalent to: not ([attribute = value]).
To match elements with specific attributes but not equal to specific values, use [attribute]: not ([attribute = value]).
Example: $(% 26#34; div [ID]: not ([id =% 26#34; test% 26#34;])% 26#34;)
4、
Usage: $(\
Note: matching a given attribute is an element starting with some values. We have seen these symbols similar to regular matching.
5、
Usage: $(% 26#34; input [name $=% 26#39; letter% 26#39;]% 26#34;) return value collection element
Description: matching a given attribute is an element that ends with some value.
6、
Usage: $(% 26#34; input [name * =% 26#39; man% 26#39;]% 26#34;) return value collection element
Description: matching a given attribute is an element that contains some values.
7、
Usage: $(% 26#34; input [ID] [name $=% 26#39; man% 26#39;]% 26#34;) return value collection element
Note: the composite attribute selector is used when multiple conditions need to be met at the same time. It is also a combination, which is often used when we actually use it. In this example, all elements containing ID attribute and whose name attribute ends in man are selected. 

% 26lt; 5% 26gt; sub element filter selectors

 1,: nth child (index / even / odd / equation)
$("div[class=one] :nth-child(2)").css("background","yellow");
Usage: $(% 26#34; UL Li: nth child (2)% 26#34;) return value set element
Note: match the nth child element under its parent element. This selector is somewhat similar to EQ () in the basic filters mentioned earlier. The difference is that the former starts from 0 and the latter starts from 1.
2、
$("div[class=one] :first-child")
Usage: $(% 26#34; UL Li: first child% 26#34;) return value collection element
Description: match the first child element.% 26#39;: first% 26#39; only matches one element, and this selector will match one child element for each parent element. Special points are needed here to remember the difference.
3、
Usage: $(% 26#34; UL Li: last child% 26#34;) return value collection element
Description: match the last child element.% 26 #39;: last% 26 #39; matches only one element, and this selector will match one child element for each parent element.
4、
Usage: $(% 26#34; UL Li: only child% 26#34;) return value collection element
Note: if an element is the only child element of the parent element, it will be matched. If the parent element contains other elements, it will not be matched

热门排行

最新文章

Powered by 世界滤芯新闻网 @2018 RSS地图 HTML地图

Copyright 365建站 © 2008-2021 华绿环保 版权所有