我有一个ID为content
的div,我想获取第一级id
元素的div
,例如。 box1
,box2
,box3
。如何才能做到这一点 ?
<div id="content">
<div id="box1" class="box1class">
<div>...</div>
</div>
<div id="box2" class="box1class">
<div>...</div>
</div>
<div id="box3" class="box1class">
<div>...</div>
</div>
</div>
最佳答案
使用>
子Seelctor。
var ids = [];
$("#content > div").each(function() {
ids.push(this.id);
});
您可以使用
map()
进一步缩短此时间:var ids = $("#content > div").map(function() {
return this.id;
}).get();