2015-02-05 118 views
2

在我的HTML中有两个具有相同类的div。如果我想按类去除div,它们都会被删除。我怎样才能(同时使用Jquery)按类去除第二个div?有没有办法让它“跳过”它找到的第一个div?删除同一类的第二个div

的代码看起来是这样的:

<div class="wrapper"> 
 
    <div class="randomdiv"> 
 
    <div class="oneofthetwodivs"> 
 
    </div> 
 
    </div> 
 
    <div class="randomdiv"></div> 
 
    <div class="randomdiv"></div> 
 
    <div class="oneofthetwodivs"> 
 
</div>

回答

5

你可以使用eq()到第二元件与该类目标(它是零基础):

$('.oneofthetwodivs').eq(1).remove(); 
+0

好使用:nth-of-type(2),这是一些快捷的服务!谢谢分配! – Brendan 2015-02-05 14:06:18

3

这应该工作:

$(".oneofthetwodivs").eq(1).remove(); 

$(".oneofthetwodivs:last").remove(); 
-1

你想在你的类

$(document).ready(function() { 
 
    $('.removeMe:nth-of-type(2)').hide(); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="removeMe">This is the first div</div> 
 
<div class="removeMe">This is the SECOND div, should not show</div> 
 
<div class="removeMe">This is the third div</div>

+0

为什么downvote? – indubitablee 2015-02-05 14:09:01