← Back to jQuery Course | Chapter 7: DOM Traversing | Lesson 3 of 10

Descendants Traverse करना

children() direct child elements select करता है।
Syntax
javascript
$(selector).children(filter);
$(selector).find(selector);

children()

.children() सिर्फ selected element के ठीक एक level नीचे के elements return करता है, आगे कहीं भी nested किसी चीज़ को skip करते हुए। यह parent() का descendant equivalent है -- सिर्फ एक step, आगे नहीं।

उदाहरण: children()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box"><p>Direct</p><div><p>Nested</p></div></div>
    <script>
      console.log($("#box").children().length);
    </script>
  </body>
</html>

find()

find() किसी भी depth पर सभी descendants search करता है, सिर्फ immediate children नहीं, जो इसे सही tool बनाता है जब कोई matching element कई levels नीचे nested हो सकता है। यह एक normal selector चलाने जैसा behave करता है, लेकिन सिर्फ उन elements के अंदर scoped जिन्हें आपने पहले select किया है।

उदाहरण: find()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box"><p>Direct</p><div><p>Nested</p></div></div>
    <script>
      console.log($("#box").find("p").length);
    </script>
  </body>
</html>

contents()

contents() text nodes और comments जैसे child nodes शामिल करता है, सिर्फ element nodes नहीं, जिन्हें children() और find() दोनों skip करते हैं। यह इसे तब उपयोगी बनाता है जब आपको specifically किसी element के अंदर raw text content inspect या manipulate करना हो।

उदाहरण: contents()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box">Some text<p>Paragraph</p></div>
    <script>
      console.log($("#box").contents().length);
    </script>
  </body>
</html>

children और find साथ में

जब आपको सिर्फ direct child elements की परवाह हो तो children() इस्तेमाल करें, और जब कोई match structure में आगे गहराई से nested हो सकता हो तो find() इस्तेमाल करें। दोनों को एक traversal chain में combine करना आपको search को exactly उस depth तक narrow करने देता है जो आपको चाहिए।

उदाहरण: children and find Together

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <form id="myForm"><div><label>Name</label></div></form>
    <script>
      console.log($("#myForm").children("div").find("label").text());
    </script>
  </body>
</html>

Descendant Practice

Descendant traversal grouped content के साथ काम करने के लिए उपयोगी है, जैसे page पर कहीं और के labels को गलती से match किए बिना किसी specific form section के अंदर हर label ढूँढना। search को किसी known container तक scope करना बहुत broad selectors से बचाता है।

उदाहरण: Descendant Practice

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="section"><label>Email</label></div>
    <script>
      console.log($("#section").find("label").length);
    </script>
  </body>
</html>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. जब target ज़्यादा गहराई से nested हो तब .children() इस्तेमाल करना, जैसे $("#box").children("p") किसी inner <div> के अंदर <p> तक पहुँचने की उम्मीद में; .children() सिर्फ एक level नीचे देखता है, इसलिए .find("p") इस्तेमाल करें।
  2. यह उम्मीद करना कि .children() या .find() text nodes या comments return करेंगे, जबकि सिर्फ .contents() उन्हें शामिल करता है।
  3. बिना किसी argument के .find() call करना और सभी descendants की उम्मीद करना, जबकि .find() को एक selector चाहिए (सब कुछ match करने के लिए .find("*") इस्तेमाल करें)।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.