The difference introduction of children () and find () based on jquery _jquery

Source: Internet
Author: User

The. Children (selector) method returns all the child elements of each element in the matching element collection ( only the son generation ). Parameters are optional, and adding parameters means filtering through the selector to filter the elements.

The. Find (selector) method returns the descendants of each element in a matching element collection. parameters are required, and you can filter the elements for selectors, jquery objects, and elements.

The. Find () is similar to the. Children () method, but the latter only traverses the single level down the DOM tree. Here's the children, I understand as a son, only at the son of this level traversal. look at the example:

Copy Code code as follows:

The. Children (selector) method returns all the child elements of each element in the matching element collection (only the Son generation). Parameters are optional, and adding parameters means filtering through the selector to filter the elements.

The. Find (selector) method returns the descendants of each element in a matching element collection. Parameters are required, and you can filter the elements for selectors, jquery objects, and elements.

The. Find () is similar to the. Children () method, but the latter only traverses the single level down the DOM tree. Here's the children, I understand as a son, only at the son of this level traversal. Look at the example:


Copy Code code as follows:

$ (' Ul.level-2 '). Children (). CSS (' background-color ', ' red ');

The result of this line of code is that project A, B, C gets a red background. Since we did not apply the selector expression, the JQuery object returned contains all the child elements. If you apply a selector, only the matching items will be included.

Take a look at an example:

Copy Code code as follows:

<script>
$ (document). Ready (function () {
$ ("#abc"). Children (". Selected"). CSS ("Color", "blue");
});
</script>
<div id= "abc" >
<span>Hello</span>
<p class= "selected" >hello again</p>
<div><--replaced by <p>
<div class= "selected" >and again</div>
<span class= "selected" >aaand again</span>
</div><--replaced by </p>
<p>and One last time</p>
</div>

The results obtained are as follows:


This is the expected result, but if the above <div> is replaced by <P>, see the code comments above, the result is:


The knowledge points that the find () method should pay attention to:

1. Find is the descendant of each element in the current element collection. As long as the match, no matter is the son generation, grandson generation all can.

2. Unlike other tree traversal methods, the selector expression is a required parameter for. Find (). If we need to implement the retrieval of all descendant elements, we can pass the wildcard selector ' * '.

3, find only in the Descendants traverse, not including themselves.

4. The selector context is implemented by the. Find () method; therefore, $ (' li.item-ii '). Find (' Li ') is equivalent to $ (' Li ', ' Li.item-ii ').

The syntax of the selector is: JQuery (selector, [context])

In general, the use of the selector for jquery is used as the first parameter. In fact, this use of the jquery () function can also pass the second argument. The purpose of passing this parameter is to limit the front selector to the context. By default, the second argument is not passed, and the selector searches the DOM from the root of the document ($ () finds the DOM element in the current HTML document), and if the second argument, such as a DOM element set or jquery object, is specified, it is looked up in the context.

Let's look at an example

Copy Code code as follows:

$ ("Div.foo"). Click (function () {
$ ("span", this). AddClass ("Bar");
});

Since we have limited the span selector to this environment, only the span in the clicked element will get the additional class.
In the interior, The selector context is implemented through the. Find () method, so $ ("span", this) is equivalent to $ (this). FIND ("span"),$ (' li.item-ii '). Find (' Li ') is equivalent to $ (' Li '), ' Li.item-ii ')

Find () More content visit: http://www.w3school.com.cn/jquery/traversing_find.asp

Children () For more information visit: http://www.w3school.com.cn/jquery/traversing_children.asp

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.