Introduction to using the jQuery filter selector :not of method

  • 2020-03-30 02:40:05
  • OfStack

JQuery (' : not selector () ')

In early versions of jQuery, the :not() filter only supported simple selectors, indicating that the selectors we passed into the filter :not could be arbitrarily complex, such as :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"}) 

So except for the p element of class is equal to a, the color of the letter p will be red.

JQuery's :not() method is jQuery's pseudo-class selector, which can filter out unwanted elements and screen out the correct results. Simply put, we have the following code:
 
$("selector1:not(selector2)") 

So let's look at the code up here, we're going to get the elements of selector1, but maybe I don't need all of them, so what do we do, filter through the :not() method, if the collection of selector1 has #1,#2,#3,#4

Our selector2 is going to filter out #4, and we're going to end up with #1,#2, and #3

A few more examples
 
$( ' li:not(:only-child)')//Matches all li except those with only one child
$( ' li:not(:first-child)');//Matches except for LI, which is the first child in its parent
$("li :not(:first)").hide();//Hide all LI except the first LI

Related articles: