I have a list where I want to add classes to every second element, counting from the first one.
我有一个列表,我想在每个第二个元素中添加类,从第一个元素开始。
Here's my list
这是我的列表
<ul class="timeline">
<span class="topbullet"></span>
<li></li>
<li></li>
<div class="clearfix"></div>
<li></li>
<li></li>
<div class="clearfix"></div>
<li></li>
<li></li>
<div class="clearfix"></div>
<li></li>
<li></li>
<div class="clearfix"></div>
<li></li>
<li></li>
<div class="clearfix"></div>
</ul>
I want to have it like this:
我想这样:
<ul class="timeline">
<span class="topbullet"></span>
<li class="odd"></li>
<li class="even"></li>
<div class="clearfix"></div>
<li class="odd"></li>
<li class="even"></li>
<div class="clearfix"></div>
<li class="odd"></li>
<li class="even"></li>
<div class="clearfix"></div>
<li class="odd"></li>
<li class="even"></li>
<div class="clearfix"></div>
<li class="odd"></li>
<li class="even"></li>
<div class="clearfix"></div>
</ul>
Here's my jQuery Code
这是我的jQuery代码
$(".timeline li:nth-of-type(2n)").addClass('even');
It's no problem to add the 'even' classes, but how to add the 'odd' classes? Which selector should I use to count from the first list element?
添加“偶数”类没有问题,但是如何添加“奇数”类呢?我应该使用哪个选择器从第一个列表元素中计数?
7 个解决方案
#1
7
You can use odd and even selector of jquery like this
可以像这样使用奇数和偶数jquery选择器
$( ".timeline li:odd" ).addClass('odd');
$( ".timeline li:even" ).addClass('even');
For your requirement you need to first odd and than even so you change this jquery like this
对于您的需求,您需要首先对奇数进行修改,甚至可以这样修改这个jquery
$( ".timeline li:odd" ).addClass('even');
$( ".timeline li:even" ).addClass('odd');
#2
2
Or JUST this
还是这
document.body.querySelectorAll(".timeline li").forEach(function(node,i){
node.classList.add(i % 2?"even":"odd");
})
#3
2
Just simply add prev()
method to your code:
只需在代码中添加prev()方法:
$(".timeline li:nth-of-type(2n)").addClass('even').prev().addClass('odd');
.even{
color:red;
}
.odd{
color: orange;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="timeline">
<span class="topbullet">Span</span>
<li>Li</li>
<li>Li</li>
<div class="clearfix">Div</div>
<li>Li</li>
<li>Li</li>
<div class="clearfix">Div</div>
<li>Li</li>
<li>Li</li>
<div class="clearfix">Div</div>
<li>Li</li>
<li>Li</li>
<div class="clearfix">Div</div>
<li>Li</li>
<li>Li</li>
<div class="clearfix">Div</div>
</ul>
#4
2
You could do this:
Iterate through the elements matching ".timeline li" and add the correct class whether its index is even or not.
您可以这样做:遍历元素匹配”。并添加正确的类,无论其索引是否为偶数。
$(".timeline li").each((index, el) => $(el).addClass(index % 2 ? 'even' : 'odd'));
#5
0
Change:
变化:
$(".timeline li:nth-of-type(2n)").addClass('even');
to:
:
$(".timeline li:nth-child(even)").addClass('even');
#6
0
If you just want to style elements you can target them using css
如果您只是想对元素进行样式化,您可以使用css对它们进行目标化
.timeline li:nth-of-type(2n + 1) {
background: red;
}
.timeline li:nth-of-type(2n) {
background: blue;
}
#7
0
You can add odd/even class like the below example.
$(document).ready(function() {
$('ul.listul li:even').addClass('even');
$('ul.listul li:odd').addClass('odd');
});
li.even {
color: #000;
}
li.odd {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="listul">
<li>list1</li>
<li>list2</li>
<li>list3</li>
<li>list4</li>
<li>list5</li>
<li>list6</li>
</ul>
#1
7
You can use odd and even selector of jquery like this
可以像这样使用奇数和偶数jquery选择器
$( ".timeline li:odd" ).addClass('odd');
$( ".timeline li:even" ).addClass('even');
For your requirement you need to first odd and than even so you change this jquery like this
对于您的需求,您需要首先对奇数进行修改,甚至可以这样修改这个jquery
$( ".timeline li:odd" ).addClass('even');
$( ".timeline li:even" ).addClass('odd');
#2
2
Or JUST this
还是这
document.body.querySelectorAll(".timeline li").forEach(function(node,i){
node.classList.add(i % 2?"even":"odd");
})
#3
2
Just simply add prev()
method to your code:
只需在代码中添加prev()方法:
$(".timeline li:nth-of-type(2n)").addClass('even').prev().addClass('odd');
.even{
color:red;
}
.odd{
color: orange;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="timeline">
<span class="topbullet">Span</span>
<li>Li</li>
<li>Li</li>
<div class="clearfix">Div</div>
<li>Li</li>
<li>Li</li>
<div class="clearfix">Div</div>
<li>Li</li>
<li>Li</li>
<div class="clearfix">Div</div>
<li>Li</li>
<li>Li</li>
<div class="clearfix">Div</div>
<li>Li</li>
<li>Li</li>
<div class="clearfix">Div</div>
</ul>
#4
2
You could do this:
Iterate through the elements matching ".timeline li" and add the correct class whether its index is even or not.
您可以这样做:遍历元素匹配”。并添加正确的类,无论其索引是否为偶数。
$(".timeline li").each((index, el) => $(el).addClass(index % 2 ? 'even' : 'odd'));
#5
0
Change:
变化:
$(".timeline li:nth-of-type(2n)").addClass('even');
to:
:
$(".timeline li:nth-child(even)").addClass('even');
#6
0
If you just want to style elements you can target them using css
如果您只是想对元素进行样式化,您可以使用css对它们进行目标化
.timeline li:nth-of-type(2n + 1) {
background: red;
}
.timeline li:nth-of-type(2n) {
background: blue;
}
#7
0
You can add odd/even class like the below example.
$(document).ready(function() {
$('ul.listul li:even').addClass('even');
$('ul.listul li:odd').addClass('odd');
});
li.even {
color: #000;
}
li.odd {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="listul">
<li>list1</li>
<li>list2</li>
<li>list3</li>
<li>list4</li>
<li>list5</li>
<li>list6</li>
</ul>