출처 : https://www.w3schools.com 사이트의 내용을 공부하며 정리하고 있다.
descentant는 자식, 손자 등 후대의 자손이다.
Traversing Down이라고 하면서 자손을 이야기하는 것보니 단순히 가로지르기, 횡단하기라는 단어로는 표현하기 힘든 부분이 있는 것 같다. 혹은 해석이 틀린 걸지도.. 세대를 거슬러간다라는 의미로 느껴진다.
오늘은 children( ), find( ) 메소드에 대해 공부해보자.
jQuery children( ) 메소드
-> children( ) 메소드는 선택된 요소의 자식 요소를 리턴한다.
이 메소드는 DOM tree의 한 단계를 내려갈 수 있다.
아래 예제는 div 요소 아래의 값을 리턴한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <!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.2.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> | cs |
chlidren 메소드에 두 번째 p에만 css 효과를 주는 예제이다.
$("div").children("p.second").css({"color": "red", "border": "2px solid red"});
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <!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.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("div").children("p.second").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> | cs |
특정 요소를 찾아서 효과를 주고 싶다면 find( ) 메소드를 사용하면 된다.
p 태그에 효과를 주는 코드이다.
$("div").find("p").css({"color": "red", "border": "2px solid red"});
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <!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.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("div").find("p").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> | cs |
모든 요소에 대해 효과를 주고 싶다면 아래 코드를 입력하자.
$("div").find("*")
'문돌이의 IT > jQuery' 카테고리의 다른 글
jQuery AJAX (1) - load()메소드 (0) | 2017.08.25 |
---|---|
jQuery Traversing 가로지르기? 횡단하기? (3) Siblings (0) | 2017.08.24 |
jQuery Traversing 가로지르기? 횡단하기? (1) ancestor (0) | 2017.08.22 |
jQuery Dimensions 면적, 치수 (0) | 2017.08.20 |
jQuery css() 메소드 (0) | 2017.08.19 |