JQuery filters not () and filter () instance code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.