Metod contents
Metod contents dobija potomke elementa,
uključujući tekst i komentare.
Sintaksa
Metod ne prihvata parametre:
.contents();
Metod contents radi slično metodu
children,
razlika se pokazuje pri pokušaju da se dobije
pristup tekstu potomaka. Uporedite dva
primeradole navedena:
Primer
Koristimo metod children:
<p id="test">text1 <b>bold</b> text2</p>
$('#test').children().each(
function() {
console.log($(this).text()); // ispisaće 'bold'
}
);
Primer
Koristimo metod contents:
<p id="test">text1 <b>bold</b> text2</p>
$('#test').contents().each(
function() {
console.log($(this).text()); // ispisaće 'text1', 'bold', 'text2'
}
);