我有一个ID为content的div,我想获取第一级id元素的div,例如。 box1box2box3。如何才能做到这一点 ?

<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();

10-05 20:30
查看更多