Differences between filter () and find () in jQuery

Source: Internet
Author: User
The difference between the filter () method and the find () method has never been quite clear. After reading jQueryCookbook, I finally figured it out, next I will share with you the difference between the filter () method and the find () method. Today I am going to read the jQuery Cookbook book and finally I will understand it.

Filter () filters DOM element packaging sets. It is used to operate the current Element Set and delete unmatched elements to obtain a new set.

The Code is as follows:


$ ('P'). filter ('. p1'); // select the p element whose class attribute is p1 in the p tag.


Find () finds the child that meets the condition in the context of the currently selected element, and returns the child element

The Code is as follows:


$ ('P'). find ('em '); // select the em Tag Element in the p tag.


As shown in the preceding figure, filter () is used to operate on the selected element set to obtain the elements that meet the conditions, while find () is used to obtain the selected element.

Child element that meets the condition.

Finally, we will introduce an end () method to return the options before destructive modifications.

The Code is as follows:


$ ('P'). filter ('. p1'). end (); // The returned result is a selection element before filter (), that is, $ ('P ')

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.