我有一个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>
答案 0 :(得分:6)
使用>
子选择器。
var ids = [];
$("#content > div").each(function() {
ids.push(this.id);
});
您可以使用map()
:
var ids = $("#content > div").map(function() {
return this.id;
}).get();
答案 1 :(得分:5)
$("#content > div")
喜欢这个。你可以像这样获得div的数组
var array = $("#content > div").map(function(){
return this.id;
}).get();
请参阅jsfiddle http://jsfiddle.net/DsyzV/
答案 2 :(得分:1)
使用:
$("#content > div").each(function() {
var divId = this.id;
});