6

我有一个 id 的 div ,content我想获取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>
4

3 回答 3

6

使用>儿童选择器。

var ids = [];
$("#content > div").each(function() {
    ids.push(this.id);
});

您可以使用以下方法进一步缩短此时间map()

var ids = $("#content > div").map(function() {
    return this.id;
}).get();
于 2012-04-10T11:04:46.157 回答
5
$("#content > div")

像这样。你可以得到这样的div数组

   var array =  $("#content > div").map(function(){
       return this.id;
    }).get();

参见 jsfiddle http://jsfiddle.net/DsyzV/

于 2012-04-10T11:04:34.973 回答
1

采用:

$("#content > div").each(function() {
  var divId = this.id;
});
于 2012-04-10T11:04:31.747 回答