The difference between the children () and the Find () of jquery traversal

Source: Internet
Author: User

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

The. Find (selector) method returns the descendants of each element in the matching element collection. Parameters are required and can be filtered for selectors, jquery object elements, and elements.

The. Find () is similar to the. Children () method, unlike the latter traversing a single level down the DOM tree only. Here children, I understand as son, only at the son this level traverse. Take a look at the following example:

[HTML]View Plaincopy
  1. <ul class="Level-1">
  2. <li class="item-i">i</li>
  3. <li class="item-ii">ii
  4. <ul class="Level-2"> not including yourself
  5. <li class="item-a">a</li>
  6. <li class="Item-b">b
  7. <ul class="level-3">
  8. <li class="item-1">1</li>
  9. <li class="item-2">2</li>
  10. <li class="item-3">3</li>
  11. </ul>
  12. </li>
  13. <li class="item-c">c</li>
  14. </ul>
  15. </li>
  16. <li class="ITEM-III">iii</li>
  17. </ul>

[JavaScript]View Plaincopy
    1. $ (' Ul.level-2 '). Children (). CSS (' background-color ', ' red ');

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

Look at an example:

[HTML]View Plaincopy
  1. <script>
  2. $ (document). Ready (function () {
  3. $ ("#abc"). Children (". Selected"). CSS ("Color", "blue");
  4. });
  5. </Script>
  6. <div id="abc">
  7. <span>hello</span>
  8. <p class="selected">hello Again</P>
  9. <div><--Change to <P>
  10. <div class="selected">and Again</div>
  11. <span class="selected">aaand Again</span>
  12. </div><--switch to </P>
  13. <p >and One last time</P>
  14. </div>

The results are as follows:

This is the expected result, but if you change the above <div> to <p>, and see the code comment above, the result is:

The. Find () method should be aware of the points of knowledge:

1. Find is the descendant of each element in the current element collection. As long as they meet, whether they are sons, grandchildren can.

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

3, find only in the descendants of the traversal, not including themselves.

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

The syntax for selectors is: jQuery (selector, [context])

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

Let's look at an example

[JavaScript]View Plaincopy
    1. $ ("Div.foo"). Click (function () {
    2. $ ("span", this ). addclass ("bar");
    3. });

Since we have limited the span selector to this environment, only the span of the clicked element will get the additional class.

the difference between the children () and the Find () of jquery traversal

The difference between the children () and the Find () of jquery traversal

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.