这是我生成的html。如何隐藏第二张孔卡?
<div id="cardContainerDealer">
<div class="card templateDealer">
<span class="dealerCardFace"></span>
<span class="dealerCardSuit"></span>
</div>
<div class="card">
<span class="dealerCardFace">8</span>
<span class="dealerCardSuit">♣</span>
</div>
<div class="card red">
<span class="dealerCardFace">10</span>
<span class="dealerCardSuit">♥</span>
</div>
</div>
</div>
我在下面尝试过,但是没有用
$("#cardContainerDealer:nth-child(2)").hide();
答案 0 :(得分:2)
您使用的nth-child
错误。尽管具有命名功能,但它会将兄弟姐妹作为元素的子元素进行匹配,因此您还需要选择类.card
:
$("#cardContainerDealer .card:nth-child(2)").hide();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="cardContainerDealer">
<div class="card templateDealer">
<span class="dealerCardFace"></span>
<span class="dealerCardSuit"></span>
</div>
<div class="card">
<span class="dealerCardFace">8</span>
<span class="dealerCardSuit">♣</span>
</div>
<div class="card red">
<span class="dealerCardFace">10</span>
<span class="dealerCardSuit">♥</span>
</div>
</div>
答案 1 :(得分:1)
使用Jquery eq
引用https://api.jquery.com/eq/
$('.card').eq(1).hide()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div id="cardContainerDealer">
<div class="card templateDealer">
<span class="dealerCardFace"></span>
<span class="dealerCardSuit"></span>
</div>
<div class="card">
<span class="dealerCardFace">8</span>
<span class="dealerCardSuit">♣</span>
</div>
<div class="card red">
<span class="dealerCardFace">10</span>
<span class="dealerCardSuit">♥</span>
</div>
</div>