[英]how to get all the children of a node as items of an array in javascript using jQuery
例如,我有以下結構:
<div class="parent">
<div>1st child</div>
<div>2nd child</div>
<div>3rd child</div>
<div>4th child</div>
</div>
我想要的是將父div的所有子節點的文本作為數組的項目。
jQuery解決方案是首選。
var a = $(".parent").children().map(function() { return $(this).text(); });
function get_this_text() {
return $(this).text();
}
$(".parent div").map(get_this_text).get()
這是小提琴(見控制台中的結果): http : //jsfiddle.net/C6pHS/
如果它可以幫助您更清楚地看到事情,也可以選擇其他答案。 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');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.