Use examples to illustrate the use of filter () and find () and the difference between children () and find () _jquery

Source: Internet
Author: User

jquery's Find () method and the filter () method are easy to confuse for beginners, where the two methods are compared and illustrated by a small example. The purpose is to understand the difference between the two methods.

HTML code

Copy Code code as follows:

<div class= "CSS" >
<p class= "Rain" > Test 1</p>
</div>
<div class= "Rain" >
<p> Test 2</p>
</div>

First Look at the Find () method
Copy Code code as follows:

$ ("div"). Find (". Rain"). CSS (' color ', ' #FF0000 ');
Equivalent to: $ ("div. Rain"). CSS (' color ', ' #ff0000 ');
Also equivalent to: $ (". Rain", "div"). css (' color ', ' #ff0000 ');

Show Results:

Question: Why does "Test 2" not change to red here?

The find () method definition: It is the descendant of each element in the current element collection, filtered by selectors, jQuery objects, or elements.

The find () method should be noted: the finding () method is found inside the current element collection. does not include himself.

The above example $ ("div") contains two elements. The first element meets the requirements. But the second element Div itself's class is "rain", but there is no class=rain inside it. So it doesn't fit.

Use the filter () method below to see what new gadgets are.

$ ("div"). Filter (". Rain"). CSS (' color ', ' #FF0000 ');

Show Results:


The filter () method defines that the filter () method reduces the collection of matching elements to elements of the specified selector.

The meaning is a collection of matching elements, with selectors testing each element itself (not descendants), the elements that match the selector are contained in the set, and the mismatch is removed (the selector is the filter). To form a new result set.

In the example above: Find () looks for the element with rain in the DIV element (descendants). and filter () is the element that filters the div's class as rain. One is a subset of its operations, and one is the filtering of its own collection elements.

Looking at two examples of the filter () method: Example of a case two

The second form of using the filter () method is to filter the elements through functions rather than selectors. For each element, if the function returns True, the element is included in the filtered collection, or the element is excluded.

Take a look at the following example:

Copy Code code as follows:

---------------------------
Messages from the Web page
---------------------------
The code is in your clipboard now
---------------------------
Are you sure
---------------------------

Copy Code code as follows:

$ (' li '). Filter (function (index) {
Return $ (' strong ', this). length = = 1;
}). css (' background-color ', ' red ');


The results are as follows:

Here's the difference between the find () method and the Children () method.

The Find () method has already said, here is not boil, below say the children () method.

Children, the superficial meaning is: children, children, children's meaning. I generally understand here as children or sons. Why is it so understood? Now let's look at the definition of children.

Children () method definition: It is to get all the child elements of each element in the matching element collection. (not including himself, only in the son generation (first level) to find)

The find () method definition: It is the descendant of each element in the current element collection , filtered by selectors, jQuery objects, or elements.

I understand that children is the child element of the "son generation" that matches each element of the collection. Find () In addition to son generation, but also include grandchildren. That is, as long as it is a descendant of the line.

Here's an example to see:

Copy Code code as follows:

<! DOCTYPE html>
<script language= "JavaScript" type= "Text/javascript" src= "Http://lock.5186.me/js/jQuery1.6.2.js" ></ Script>
<style>
body {font-size:16px; font-weight:bolder;}
p {margin:5px 0;}
</style>
<body>
<div>
<span>Hello</span>
<span>
<p class= "Selected" >test</p>
</span>
<p>and One last time</p>
<p class= "Selected" >again</p>
</div>
<p class= "selected" >hello again</p>
<script>$ ("div"). Children (". Selected"). CSS ("Color", "blue");</script>
</body>

the results are shown in the following illustration:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.