2015-02-05 84 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

相關問題