我的 html 中有这样的内容:
<div class="onlyContent">
<!-- some more stuff here -->
</div>
<div class="onlyContent">
<!-- some more stuff here -->
</div>
现在,使用 jQuery
我想从 dom 中删除第二次出现的 onlyContent
HTML 类。
最终结果应该是这样的:
<div class="onlyContent">
<!-- some more stuff here -->
</div>
我发现你可以以某种方式使用nth-child
选择器,但尝试以这种方式访问它并没有为我做到
$('.onlyContent:nth-child(2)').remove();
最佳答案
您可以使用 :eq(1)
来定位匹配集中的第二个元素:
$('.onlyContent:eq(1)').remove();
如果元素数量超过两个,则应使用 :not(:first)
或 :gt(0)
:
$('.onlyContent:not(:first)').remove();
或
$('.onlyContent:gt(0)').remove();
关于javascript - jquery 删除元素,如果它出现两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30371831/