addClass每n个 [英] addClass every nth

查看:78
本文介绍了addClass每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>

我可以做2:odd / even,但是如何做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.

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

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