JQuery Traversal – Filtering

Source: Internet
Author: User

Narrowing the range of search elements

The three most basic filtering methods are: First (), Last (), and EQ (), which allow you to select a specific element based on its position within a set of elements.

Other filtering methods, such as filter () and not (), allow you to select elements that match or do not match a specified criterion.

JQuery First () method

The first () method returns the initial element of the selected element.

The following example selects the first <p> element inside the first <div> element:

Instance $ (document). Ready (function () {
$ ("div p"). First ();
});
Try it?

JQuery Last () method

The last () method returns the final element of the selected element.

The following example selects the last <p> element in the last <div> element:

Instance $ (document). Ready (function () {
$ ("div p"). Last ();
});
Try it?

JQuery eq () method

The EQ () method returns the element with the specified index number in the selected element.

The index number starts at 0, so the first element's index number is 0 instead of 1. The following example selects a second <p> element (index number 1):

Instance $ (document). Ready (function () {
$ ("P"). EQ (1);
});
Try it?

JQuery Filter () method

The filter () method allows you to specify a standard. Elements that do not match this standard are removed from the collection, and the matching elements are returned.

The following example returns all the <p> elements with the class name "Intro":

Instance $ (document). Ready (function () {
$ ("P"). Filter (". Intro");
});
Try it?

JQuery not () method

The Not () method returns all elements that do not match the criteria.

Tip: The Not () method is the opposite of filter ().

The following example returns all the <p> elements without the class name "Intro":

Instance $ (document). Ready (function () {
$ ("P"). Not (". Intro");
});
Try it?

JQuery Traversal – Filtering

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.