Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade For Teachers Spaces Get Certified Upgrade For Teachers Spaces
   ❮     
     ❯   

jQuery Traversing -Descendants


With jQuery you can traverse down the DOM tree to find descendants of an element.

A descendant is a child, grandchild, great-grandchild, and so on.


Traversing Down the DOM Tree

Two useful jQuery methods for traversing down the DOM tree are:

  • children()
  • find()

jQuery children() Method

Thechildren() method returns all direct children of the selected element.

This method only traverses a single level down the DOM tree.

The following example returns all elements that are direct children of each<div> elements:

Example

$(document).ready(function(){
  $("div").children();
});
Try it Yourself »

You can also use an optional parameter to filter the search for children.

The following example returns all<p> elements with the class name "first", that are direct children of<div>:

Example

$(document).ready(function(){
  $("div").children("p.first");
});
Try it Yourself »


jQuery find() Method

Thefind() method returns descendant elements of the selected element, all the way down to the last descendant.

The following example returns all<span> elements that are descendants of<div>:

Example

$(document).ready(function(){
  $("div").find("span");
});
Try it Yourself »

The following example returns all descendants of<div>:

Example

$(document).ready(function(){
  $("div").find("*");
});
Try it Yourself »

jQuery Exercises

Test Yourself With Exercises

Exercise:

Use a jQuery method to get all direct children of a <div> element element.

$("div").();

Start the Exercise


jQuery Traversing Reference

For a complete overview of all jQuery Traversing methods, please go to ourjQuery Traversing Reference.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp