Home >Web Front-end >JS Tutorial >_jquery comes to mind from jquery's filter.filter() method
Recently I discovered jquery's .filter() method. This is a very powerful method. The most powerful thing is that it can accept a function as a parameter, and then judge based on the return value of the function. If the return value is true, this element will be retained. If the return value is false, the element will be removed. This is the filter of jquery selector.
It’s useless to talk, let’s show it
Hello, how old are you today
Why should I tell you
element that has a child element, I usually write like this
$("p>span").parent();
Now with the help of .filter() we can write like this:
$("p").filter(function(index){
return $(this).find("span").size();
});
Although it seems a bit cumbersome, it provides us with an extremely powerful function to customize the search for elements. We can pass a function and then use jquery's implicit iteration to implement the search.
This is the most flexible method of finding elements provided by jquery that I have seen so far.