Dengan jQuery kita bisa melintasi DOM tree untuk menemukan descendants dari sebuah elemen. Show Descendant adalah child, grandchild, great-grandchild dan lain sebagainya. Melintasi DOM treeDua metode jQuery yang berguna untuk menelusuri pohon DOM adalah:
Metode jQuery children()Metode Metode ini hanya melintasi satu tingkat di bawah pohon DOM. Contoh berikut mengembalikan semua elemen yang merupakan descendants langsung dari setiap elemen Contoh : <!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.5.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 (elemen saat ini) <p>p (child) <span>span (grandchild)</span> </p> <p>p (child) <span>span (grandchild)</span> </p> </div> </body> </html> Kita juga bisa menggunakan parameter opsional untuk memfilter pencarian children. Contoh berikut mengembalikan semua Contoh : <!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.5.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 (element saat ini) <p class="first">p (child) <span>span (grandchild)</span> </p> <p class="second">p (child) <span>span (grandchild)</span> </p> </div> </body> </html> Metode jQuery find()Metode Contoh berikut mengembalikan semua elemen Contoh : <!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.5.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 (elemen saat ini) <p>p (child) <span>span (grandchild)</span> </p> <p>p (child) <span>span (grandchild)</span> </p> </div> </body> </html> Contoh berikut mengembalikan semua descendants Contoh : <!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.5.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 (elemen saat ini) <p>p (child) <span>span (grandchild)</span> </p> <p>p (child) <span>span (grandchild)</span> </p> </div> </body> </html> I came across a question stating: Which on of the following is a Descendant operator: WHICH IS IT? AND PLEASE EXPLAIN WHY OR REFER ANY ARTICLE OR LINK asked Feb 16, 2020 at 5:17
The answer is A. The
answered Feb 16, 2020 at 5:36
ChickenMinhChickenMinh 391 gold badge2 silver badges11 bronze badges 3 Not the answer you're looking for? Browse other questions tagged javascript operator-keyword or ask your own question. |