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 ')