每 n 次添加一次 [英] addClass every nth

查看:36
本文介绍了每 n 次添加一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个想要以 3 种不同方式设置样式的元素列表.

I have a list of elements that I want to style in 3 different ways.

我希望每个第三个列表项在整个列表中都具有相同的类.

I want every 3rd list item to have the same class throughout the whole list.

例如:

<li class="A">Some Content</li>
<li class="B">Some Content</li>
<li class="C">Some Content</li>
<li class="A">Some Content</li>
<li class="B">Some Content</li>
<li class="C">Some Content</li>
<li class="A">Some Content</li>
<li class="B">Some Content</li>
<li class="C">Some Content</li>

我可以用 :odd/even 做 2,但是如何用 3 做呢?

I can do 2 with :odd/even, but how to do it with 3?

推荐答案

试试

$("ul li:nth-child(3n+1)").addClass("A")
$("ul li:nth-child(3n+2)").addClass("B")
$("ul li:nth-child(3n)").addClass("C")

随意合并它以使其更漂亮,但我想公开选择器.

Feel free to consolidate it to make it prettier, but I wanted to expose the selectors.

这篇关于每 n 次添加一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆