jQuery에서 자식 요소를 다루는 children 메서드
요소의 자식 요소를 다루기 위해, 먼저 요소의 직접적인 자식 요소만 얻을 수 있는
children
메서드를 살펴보겠습니다.
다음 HTML 코드를 살펴보세요:
<p id="test">text1 <b>bold</b> text2</p>
자식 요소를 얻기 위해 메서드에 다음과 같은 함수를 작성합니다:
$('#test').children().each(
function() {
console.log($(this).text()); // 'bold'를 출력합니다
}
);
다음 태그들을 가진 div가 있습니다:
<div id="text"><i>text1</i> <b>text2</b> text3 text4</div>
children 메서드를 사용하여 이 div의 모든 직접적인
자식 요소를 콘솔에 출력하세요.