如何只获得第一级div?

use*_*100 6 html javascript jquery jquery-selectors

我有一个id为div的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>
Run Code Online (Sandbox Code Playgroud)

Ror*_*san 6

使用>子seelctor.

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

您可以通过使用进一步缩短这个map():

var ids = $("#content > div").map(function() {
    return this.id;
}).get();
Run Code Online (Sandbox Code Playgroud)


Chu*_*ris 5

$("#content > div")
Run Code Online (Sandbox Code Playgroud)

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

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

请参阅jsfiddle http://jsfiddle.net/DsyzV/