我有一个Wordpress博客,显示了20个帖子。
我想为每个(2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20)添加一个类
所以我不想将类添加到(1,2和10,11)
我用选择器nth-child(3n + 1)尝试了css ......这不是解决方案。
$('.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 :(得分:0)
将要跳过的索引添加到以下数组中。 Plz注意到我使用了基于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>