[英]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 the parent div as items of an array. 我想要的是将父div的所有子节点的文本作为数组的项目。
the jQuery solution is preferred. jQuery解决方案是首选。
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/ 这是小提琴(见控制台中的结果): http : //jsfiddle.net/C6pHS/
Optionally to the other answers could also do this if it helps you to see things more clearly. 如果它可以帮助您更清楚地看到事情,也可以选择其他答案。 HTML:
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: JQuery的:
var childArray = $('.child');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.