JQuery filters not () and filter () instance code _jquery

Source: Internet
Author: User
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

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.