Method 1:
Copy codeThe Code is as follows:
$ (Function (){
$ ("Li"). not (": even" ).css ("color", "red ");
$ ("Li"). filter (": odd" ).css ("color", "red ");
})
Method 2:
Copy codeThe Code is as follows:
$ (Function (){
$ ("Li"). filter (function (index ){
Return index % 2 = 0;
)).Css ("color", "red ");
$ ("Li"). not (function (index ){
Return index % 2! = 0;
)).Css ("color", "red ");
})
Both methods can achieve the same effect. not is the opposite of filter!
JQuery filter selector: not () method Introduction
JQuery (': not (selector )')
In earlier versions of jQuery, The: not () filter only supports Simple selectors, which means that the selector passed into the: not filter can be any complicated, for example: not (div) and: not (div,)
<P> "a"> sdfsdfs </p>
<P> "B"> sdfsdfs </p>
<P> "c"> sdfsdfs </p>
$ ("P: not (. a)" ).css ({"color": "red "})
In addition to the p element whose class is equal to a, the text color of other P becomes red.
: Not () pseudo-class filtering selector, which is called the real interface. jQuery's: not () method is jQuery's pseudo-class selector, which can filter unwanted elements, filter out the correct results. In short, we have the following code:
$ ("Selector1: not (selector2 )")
After analyzing the code above, we need to obtain the elements of selector1, but I may not need all of them. What should I do? Use the: not () method to filter them if the set of selector1 contains #1, #2, #3, #4
Our selector2 is to filter out #4. The above code will eventually get #1, #2, #3
A few more columns
$ ('Li: not (: only-child) ') // matches all li, except
$ ('Li: not (: first-child) '); // matches the li
$ ("Li: not (: first)"). hide (); // hide all LI addresses except the first LI