选择nextAll包括n-child条件

时间:2011-05-05 23:10:07

标签: javascript jquery

我如何选择第n个孩子加上其他div.containers我试过nextAll但跳过nth-child

$('.container nth-child(' + index + ')').nextAll().each(function() {
    //problem here in selecting the next index ^^ plus the rest of div.container
});

<div>
    <div class="container" style="width:220px; height:220px; z-index:999; position:absolute; background-color:Aqua; float:left">
        <div class="menu-item">Menu Item 1</div>
        <div class="menu-item">Menu Item 2</div>
        <div class="menu-item">Menu Item 3</div>
    </div>
    <div class="container" style="width:200px; height:200px; z-index:998; position:absolute; background-color:Blue; float:left">
        <div class="menu-item">Menu Item 4</div>
        <div class="menu-item">Menu Item 5</div>
        <div class="menu-item">Menu Item 6</div>
    </div>
    <div class="container" style="width:180px; height:180px; z-index:997; position:absolute; background-color:Fuchsia; float:left">
        <div class="menu-item">Menu Item 7</div>
        <div class="menu-item">Menu Item 8</div>
        <div class="menu-item">Menu Item 9</div>
    </div>
    <div class="container" style="width:160px; height:160px; z-index:997; position:absolute; background-color:Green; float:left">
        <div class="menu-item">Menu Item 10</div>
        <div class="menu-item">Menu Item 11</div>
        <div class="menu-item">Menu Item 12</div>
    </div>
</div>

1 个答案:

答案 0 :(得分:3)

是的,您需要将选择器.andSelf()添加到每个之前,以包含原始选择器,如下所示:

$('.container nth-child(' + index + ')').nextAll().andSelf().each(function()

应该为你做。这是jQuery参考:http://api.jquery.com/andSelf/