The first way to write:
$ (function(){
$("li").not(":even").css("color","red");
$("li").filter(":odd" ).css("color","red");
})
The second way of writing:
$(function(){
$("li").filter(function(index ) {
return index%2 == 0;
}).css("color","red");
$("li").not(function(index) {
return index%2 !== 0;
}).css("color","red");
})
Both of these writing methods can be used To achieve the same effect, not and filter are opposite filters!
jQuery filter selector:not() method introduction
jQuery(':not(selector)')
In earlier versions of jQuery, the :not() filter only supports A simple selector means that the selector we pass into the :not filter can be arbitrarily complex, such as: not(div a) and :not(div,a)
"a" >sdfsdfs
"b">sdfsdfs
"c">sdfsdfs
$ ("p:not(.a)").css({"color":"red"})
Then except for the p element whose class is equal to a, the text color of other P will become red.
:not() pseudo-class filter selector. This name is really hard to pronounce. jQuery’s :not() method is jQuery’s pseudo-class selector. It can filter out unnecessary elements and filter out the correct results. Simply put, we There is the following code:
$("selector1:not(selector2)")
Let’s analyze the above code. We want to get the elements of selector1, but maybe I don’t need them all. What should I do? Pass: not() method to filter, if there are #1, #2, #3, #4 in selector1's collection
Our selector2 is to filter out #4. With the above code, we will eventually get #1, #2, #3
A few more examples
$('li:not(:only-child)')//Match all li except
$('li which has only one child element :not(:first-child)');//Match LI
$("li :not(:first)").hide();// Hide all LI except the first LI