메서드 contents
메서드 contents는 텍스트와 주석을 포함하여
요소의 자식들을 가져옵니다.
문법
이 메서드는 매개변수를 받지 않습니다:
.contents();
메서드 contents는
children 메서드와
유사하게 동작하지만, 자식의 텍스트에 접근하려고 할 때
차이가 나타납니다. 아래의 두 예제를 비교해 보세요:
예제
메서드 children 사용:
<p id="test">text1 <b>bold</b> text2</p>
$('#test').children().each(
function() {
console.log($(this).text()); // 'bold'를 출력함
}
);
예제
메서드 contents 사용:
<p id="test">text1 <b>bold</b> text2</p>
$('#test').contents().each(
function() {
console.log($(this).text()); // 'text1', 'bold', 'text2'를 출력함
}
);