how to get all the children of a node as items of an array in javascript using jQuery
for example I have the following structure:
<div class="parent">
<div>1st child</div>
<div>2nd child</div>
<div>3rd child</div>
<div>4th child</div>
</div>
all I want is to have the text of all the children of 开发者_如何学Gothe parent div as items of an array.
the jQuery solution is preferred.var a = $(".parent").children().map(function() { return $(this).text(); });
function get_this_text() {
return $(this).text();
}
$(".parent div").map(get_this_text).get()
Here's it on fiddle (see result in your console): http://jsfiddle.net/C6pHS/
Optionally to the other answers could also do this if it helps you to see things more clearly. HTML:
<div class="parent">
<div class="child">1st child</div>
<div class="child">2nd child</div>
<div class="child">3rd child</div>
<div class="child">4th child</div>
</div>
JQuery:
var childArray = $('.child');
精彩评论