更新时间:2023-12-03 08:09:40
$("#list > li").on("click",function(){
$('.red').removeClass('red');
if($(this).index() % 2 == 0) {
$('#list > li:even').addClass('red');
} else {
$('#list > li:odd').addClass('red');
}
});
使用 $(this).index 0
来确定被点击的元素是偶数还是奇数,然后分别使用 :even
/ :odd
。
Use $(this).index() % 2 == 0
to figure out whether the element clicked is even or odd. Then just add the class respectively using :even
/:odd
.