The first method of writing:
Copy Code code as follows:
$ (function () {
$ ("Li"). Not (": even"). CSS ("Color", "red");
$ ("Li"). Filter (": Odd"). CSS ("Color", "red");
})
The second way:
Copy Code code 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");
})
These two kinds of writing, can achieve the same effect, not and filter is the opposite filter!
jquery Filter Selector: Not () method introduction
JQuery (': Not (selector) ')
In earlier versions of jquery, the not () filter only supported a simple selector, stating that we passed in: not the selector in this filter can be arbitrarily complex, for example: not (div a) and:not (div,a)
<p > "a" >sdfsdfs</p>
<p > "B" >sdfsdfs</p>
<p > "C" >sdfsdfs</p>
$ ("P:not (. a)"). CSS ({"Color": "Red"})
In addition to the P element of class equals A, the text color of the other p becomes red.
: Not () pseudo-class filter selector, which is called a really clumsy, jquery: Not () method is jquery's pseudo class selector, can filter unwanted elements, filter out the correct results, simply say we have the following code:
$ ("Selector1:not (Selector2)")
We analyze the code above, we want to get the elements of Selector1, but maybe I do not need all, how to do, through: not () method to filter, if Selector1 in the collection of #, #2, #3, #4
Our Selector2 is to filter out #4, the above code we will eventually get a #, #2, #3
Give me a few more columns.
$ (' Li:not (: only-child) ')/Match all Li, except for only one child element
$ (' Li:not (: first-child) ');//Match except for Li that is the first child element in his parent element
$ ("Li:not (: First)"). Hide ();//Hides all li except the first Li