Home  >  Article  >  Web Front-end  >  _jquery comes to mind from jquery’s filter.filter() method

_jquery comes to mind from jquery’s filter.filter() method

WBOY
WBOYOriginal
2016-05-16 17:21:041076browse

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

Copy the code The code is as follows:


Hello, how old are you today


Why should I tell you


If we want to select a

element that has a child element, I usually write like this

Copy code The code is as follows:

$("p>span").parent();

Now with the help of .filter() we can write like this:
Copy the code The code is as follows:

$("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.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn