Home > Web Front-end > JS Tutorial > _jquery comes to mind from jquery's filter.filter() method

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

WBOY
Release: 2016-05-16 17:21:04
Original
1093 people have browsed it

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.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template