Home > Web Front-end > JS Tutorial > jQuery filter not() and filter() example code_jquery

jQuery filter not() and filter() example code_jquery

WBOY
Release: 2016-05-16 17:53:55
Original
1141 people have browsed it
The first way to write:
Copy the code The code is as follows:

$ (function(){
$("li").not(":even").css("color","red");
$("li").filter(":odd" ).css("color","red");

})

The second way of writing:
Copy code The 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 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
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template