Recently, jquery's. the filter () method is really a very powerful method. The most powerful thing is that it can take 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 returned value is false, this element will be removed. This is the filter of the jquery selector.
It's useless. Let's show it.
Copy codeThe Code is as follows:
<Body>
<P> hello, how old is today? </p>
<P> <span> Why </span> </p>
</Body>
If we want to select a <span> sub-element <p> element, I usually write it like this.
Copy codeThe Code is as follows:
$ ("P> span"). parent ();
Now we can use. filter () to write it like this:
Copy codeThe Code is as follows:
$ ("P"). filter (function (index ){
Return $ (this). find ("span"). size ();
});
Although it seems a little troublesome, it provides an extremely powerful feature for Custom Search elements. We can pass a function and use jquery's implicit iteration to implement search.
This is the most flexible method for searching elements that I have seen in jquery.