Recently discovered jquery's. Filter () method, which is a very powerful method, the most powerful is that he can accept a function as a parameter, and then based on the return value of the function to determine if the return value is true, this element will be retained, if the return value is false, This element will be removed. This is the jquery selector filter.
Empty say useless, let's show
Copy Code code as follows:
<body>
<p> Hello, how old are you today </p>
<p><span> why to tell you </span></p>
</body>
If we're going to choose a <p> element that has a <span> element, that's what I usually write.
Copy Code code as follows:
$ ("P>span"). Parent ();
Now with the help of. Filter () We can write this way:
Copy Code code as follows:
$ ("P"). Filter (function (index) {
Return $ (a). FIND ("span"). Size ();
});
Although it may seem like a bit of a hassle, he provides us with a very powerful custom lookup element, we can pass a function and then use the implicit iteration of jquery to find it.
This is the most flexible way to find elements that I see in jquery today.