第20章 jQueryのトラバース - 子孫
jQuery(ジェイクエリー)は、ウェブブラウザ用のJavaScriptコードをより容易に記述できるようにするために設計されたJavaScriptライブラリである。
homepage
# **jQueryのトラバース - 子孫** *** jQueryを使えば、DOMツリーをたどって、要素の子孫を見つけることができます。 子孫は、子、孫、曽孫などです。 ## **1.DOMツリーを下にたどる** DOMツリーを下にたどるのに2つの便利なjQueryメソッドがあります。 * `children()` * `find()` ## **2.children()メソッド** このchildren()メソッドは、選択された要素の直接の子すべてを返します。 このメソッドは、DOMツリーの下の1つのレベルをたどるだけです。 次の例では、各`<div>`要素の直接の子であるすべての要素を返します。 #### **例** [sample20-1.html] ``` <!DOCTYPE html> <html> <head> <style> .descendants * { display: block; border: 2px solid lightgrey; color: lightgrey; padding: 5px; margin: 15px; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("div").children().css({"color": "red", "border": "2px solid red"}); }); </script> </head> <body> <div class="descendants" style="width:500px;">div (current element) <p>p (child) <span>span (grandchild)</span> </p> <p>p (child) <span>span (grandchild)</span> </p> </div> </body> </html> ``` #### **実行結果**  オプションのパラメータを使用して、子供の検索をフィルタすることもできます。 次の例では`<p>`、 "first"というクラス名を持つすべての要素が返されます。これは、その直接の子です`<div>`。 #### **例** [sample20-2.html] ``` <!DOCTYPE html> <html> <head> <style> .descendants * { display: block; border: 2px solid lightgrey; color: lightgrey; padding: 5px; margin: 15px; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("div").children("p.first").css({"color": "red", "border": "2px solid red"}); }); </script> </head> <body> <div class="descendants" style="width:500px;">div (current element) <p class="first">p (child) <span>span (grandchild)</span> </p> <p class="second">p (child) <span>span (grandchild)</span> </p> </div> </body> </html> ``` #### **実行結果**  ## **3.find()メソッド** この`find()`メソッドは、最後の子孫まで、選択した要素の子孫要素を返します。 次の例では、`<span>`その子孫であるすべての要素が返されます`<div>`。 #### **例** [sample20-3.html] ``` <!DOCTYPE html> <html> <head> <style> .descendants * { display: block; border: 2px solid lightgrey; color: lightgrey; padding: 5px; margin: 15px; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("div").find("span").css({"color": "red", "border": "2px solid red"}); }); </script> </head> <body> <div class="descendants" style="width:500px;">div (current element) <p>p (child) <span>span (grandchild)</span> </p> <p>p (child) <span>span (grandchild)</span> </p> </div> </body> </html> ``` #### **実行結果**  次の例では、`<div>`のすべての子孫が返されます。 #### **例** [sample20-4.html] ``` <!DOCTYPE html> <html> <head> <style> .descendants * { display: block; border: 2px solid lightgrey; color: lightgrey; padding: 5px; margin: 15px; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("div").find("*").css({"color": "red", "border": "2px solid red"}); }); </script> </head> <body> <div class="descendants" style="width:500px;">div (current element) <p>p (child) <span>span (grandchild)</span> </p> <p>p (child) <span>span (grandchild)</span> </p> </div> </body> </html> ``` #### **実行結果** 
content
戻る