2016-05-19 53 views
0

我有一個WordPress的博客顯示20職位。jQuery選擇每3,4,5,6,7和addClass - 但重複它

我想給每個類添加一個類(2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20)

,所以我不希望將類添加到(1,2和10,11)

我試着用CSS選擇第n個孩子(3N + 1)......這不是解決辦法。

$('.article-box:nth-child(1n+3)').addClass('small-river'); 

我也試過.slice(2,3,4,5,6,7,8)等......但我無法得到這個重複。

$('.article-box').slice(3,4,5).addClass('small-river'); 
+0

while循環添加計數器變量,當它到達這些數字那麼它就可以如果達到這些數字,那麼你必須的功能發生 –

回答

0

添加索引你想跳到下面的數組。請注意,我使用0爲基礎的索引。

var skipIndex = [1, 2, 10, 11]; 
 

 
$('.article-box').each(function(index) { 
 
    
 
    if(skipIndex.indexOf(index) == -1) { 
 
    $(this).addClass('small-river'); 
 
    } 
 
    
 
});
.small-river { 
 
    background: #0f0; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
    <div class="article-box">Lorem ipsum</div> 
 
    <div class="article-box">Lorem ipsum</div> 
 
    <div class="article-box">Lorem ipsum</div> 
 
    <div class="article-box">Lorem ipsum</div> 
 
    <div class="article-box">Lorem ipsum</div> 
 
    <div class="article-box">Lorem ipsum</div> 
 
    <div class="article-box">Lorem ipsum</div> 
 
    <div class="article-box">Lorem ipsum</div> 
 
    <div class="article-box">Lorem ipsum</div> 
 
    <div class="article-box">Lorem ipsum</div> 
 
    <div class="article-box">Lorem ipsum</div> 
 
    <div class="article-box">Lorem ipsum</div> 
 
    <div class="article-box">Lorem ipsum</div> 
 
    <div class="article-box">Lorem ipsum</div>