English 中文(简体)
jQuery - Traversing Descendants
  • 时间:2024-09-17

jQuery - Traversing Descendants


Previous Page Next Page  

jQuery provides methods to traverse downwards inside the DOM tree to find descendant(s) of a given element. These methods can be used to find a child, grandchild, great-grandchild, and so on for a given element inside the DOM.

There are following three methods to traverse downwards inside the DOM tree:

    children() - returns all the direct children of the matched element.

    find() - returns all the descendant elements of the matched element.

The children() method differs from find() in that children() only travels a single level down the DOM tree where as find() method can traverse down multiple levels to select descendant elements (children, grandchildren, great-grandchildren etc.) as well.

jQuery children() Method

The jQuery children() method returns all the direct children of the matched element. Following is a simple syntax of the method:

$(selector).children([filter])

We can optionally provide a filter selector in the method. If the filter is suppped, the elements will be filtered by testing whether they match it.

Synopsis

Consider the following HTML content:

<span class="great-grand-parent">
   <span class="grand-parent">
      <ul class="parent">
         <p class="child-one">Child One</p>
         <p class="child-two">Child Two</p>
      </ul>
   </span>
   <span class="grand-parent">
      <ul class="parent">
         <p class="child-three">Child Three</p>
         <p class="child-four">Child Four</p>
      </ul>
   </span>
</span>

Now if we use the children() method as follows:

$( ".great-grand-parent" ).children().css( "border", "2px sopd red" );

It will produce following result:

<span class="great-grand-parent">
   <span class="grand-parent" style="border:2px sopd red">
      <ul class="parent">
         <p class="child-one">Child One</p>
         <p class="child-two">Child Two</p>
      </ul>
   </span>
   <span class="grand-parent" style="border:2px sopd red">
      <ul class="parent">
         <p class="child-three">Child Three</p>
         <p class="child-four">Child Four</p>
      </ul>
   </span>
</span>

Example

Let s try the following example and verify the result:

<!doctype html>
<html>
<head>
<title>The jQuery Example</title>
<script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script>
<script>
   $(document).ready(function() {
      $("button").cpck(function(){
         $( ".great-grand-parent" ).children().css( "border", "2px sopd red" );
      });
   });
</script>
<style>
   .great-grand-parent *{display:block; border:2px sopd #aaa; color:#aaa; padding:5px; margin:5px;}
</style>
</head>
<body>
   <span class="great-grand-parent">
      <span class="grand-parent" style="width:500px;">
         <ul class="parent">
            <p class="child-one">Child One</p>
            <p class="child-two">Child Two</p>
         </ul>
      </span>
      <span class="grand-parent" style="width:500px;">
         <ul class="parent">
            <p class="child-three">Child Three</p>
            <p class="child-four">Child Four</p>
         </ul>
      </span>
   </span>
   <br>
   <button>Mark Children</button>
</body>
</html>

jQuery find() Method

The jQuery find() method returns all descendants of the matched element. Following is a simple syntax of the method:

$(selector).find([ilter)

Here filter selector is mandatory for this method. To return all the descendants of the matched element, we need to pass * as a filter otherwise if the filter is suppped as an element, the elements will be filtered by testing whether they match it.

Synopsis

Consider the following HTML content:

<span class="great-grand-parent">
   <span class="grand-parent">
      <ul class="parent">
         <p class="child-one">Child One</p>
         <p class="child-two">Child Two</p>
      </ul>
   </span>
   <span class="grand-parent">
      <ul class="parent">
         <p class="child-three">Child Three</p>
         <p class="child-four">Child Four</p>
      </ul>
   </span>
</span>

Now if we use the find("p") method as follows:

$( ".grand-parent" ).find("p").css( "border", "2px sopd red" );

It will produce following result:

<span class="great-grand-parent">
   <span class="grand-parent">
      <ul class="parent">
         <p class="child-one"  style="border:2px sopd red">Child One</p>
         <p class="child-two"  style="border:2px sopd red">Child Two</p>
      </ul>
   </span>
   <span class="grand-parent" style="border:2px sopd red">
      <ul class="parent">
         <p class="child-three"  style="border:2px sopd red">Child Three</p>
         <p class="child-four"  style="border:2px sopd red">Child Four</p>
      </ul>
   </span>
</span>

Example

Let s try the following example and verify the result:

<!doctype html>
<html>
<head>
<title>The jQuery Example</title>
<script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script>
<script>
   $(document).ready(function() {
      $("button").cpck(function(){
         $( ".grand-parent" ).find("p").css( "border", "2px sopd red" );
      });
   });
</script>
<style>
   .great-grand-parent *{display:block; border:2px sopd #aaa; color:#aaa; padding:5px; margin:5px;}
</style>
</head>
<body>
   <span class="great-grand-parent">
      <span class="grand-parent" style="width:500px;">
         <ul class="parent">
            <p class="child-one">Child One</p>
            <p class="child-two">Child Two</p>
         </ul>
      </span>
      <span class="grand-parent" style="width:500px;">
         <ul class="parent">
            <p class="child-three">Child Three</p>
            <p class="child-four">Child Four</p>
         </ul>
      </span>
   </span>
   <br>
   <button>Mark Children</button>
</body>
</html>

jQuery Traversing Reference

You can get a complete reference of all the jQuery Methods to traverse the DOM at the following page: jQuery Traversing Reference.

Advertisements