jQuery在div中选择x div

时间:2016-01-26 06:53:44

标签: javascript jquery html

我想在div.first

中隐藏()第二和第三个div和第二个div
<div class="first">
    <div class="another"></div>
    <div class="second"></div>
    <div class="second"></div>  // Hide this
    <div class="second"></div> // Hide this
    <div class="second"></div>
</div>
<div class="first">
    <div class="another"></div>
    <div class="second"></div>
    <div class="second"></div> // Hide this
    <div class="second"></div> // Hide this
    <div class="second"></div>
</div>

如何在头等舱中选择每个第2和第3个div 用jquery?

3 个答案:

答案 0 :(得分:2)

尝试使用.each().slice()

$(".first").each(function() { $(".second", this).slice(1,3).hide() })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div class="first">
    <div class="another"></div>
    <div class="second">first</div>
    <div class="second">second</div>  // Hide this
    <div class="second">third</div> // Hide this
    <div class="second">fourth</div>
</div>
<div class="first">
    <div class="another"></div>
    <div class="second">first</div>
    <div class="second">second</div>  // Hide this
    <div class="second">third</div> // Hide this
    <div class="second">fourth</div>
</div>

答案 1 :(得分:1)

您可以使用:lt:gt选择器。

$('.second:gt(0):lt(2)', '.first').hide();

Fiddle

答案 2 :(得分:0)

您可以nth-child使用

$('.second:nth-child(3), .second:nth-child(4)', '.first').hide();

&#13;
&#13;
$('.second:nth-child(3), .second:nth-child(4)', '.first').hide();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="first">
    <div class="another">1</div>
    <div class="second">2</div>
    <div class="second">3</div>  <!-- Hide this -->
    <div class="second">4</div>  <!-- Hide this -->
    <div class="second">5</div>
</div>
<div class="first">
    <div class="another">1</div>
    <div class="second">2</div>
    <div class="second">3</div>  <!-- Hide this -->
    <div class="second">4</div>  <!-- Hide this -->
    <div class="second">5</div>
</div>
&#13;
&#13;
&#13;