如何将多个类添加到 ul 列表
如何将多个类添加到 ul 列表中?我不想将其添加到每个 li 项目中,而只是其中一些项目。
编辑:
我想用 jQuery 添加类。像这样:
<ul>
<li>something1</li>
<li>something2</li>
<li>something3</li>
<li>something4</li>
<li>something5</li>
</ul>
现在我想在第二个之后的每个里添加类(第三个,第四个和第五个里元素)。我知道如何添加课程,但我怎样才能让它在每个里重复。这确实是我的问题。
How can I add several classes to ul list? I don't want to add it to every li items but just some of them.
EDIT:
I want to add classes with jQuery. Like this:
<ul>
<li>something1</li>
<li>something2</li>
<li>something3</li>
<li>something4</li>
<li>something5</li>
</ul>
And now I want to add class every li after the second one (3rd, 4th and 5th li element). I know how to add class but how can i make it to repeat in each li. Thats my problem really.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,要向
ul
中的每个li
添加 3 个类,您需要这样做:因为您没有为
li
指定任何条件你想要获得附加类的元素,我不得不猜测:这会将这三个类添加到每隔一个
li
(更准确地说,是零索引数组中的偶数元素) 。祝你好运。
Well, to add 3 classes to every
li
in aul
, you'd do:Since you don't specify any criteria for the
li
elements you want to get the additional classes, I'm forced to guess:That will add the three classes to every other
li
(the even numbered elements in a zero-indexed array, to be more precise).Good luck.
您的问题不够详细,但一般来说您需要根据需要在特定 li 元素上设置类。
例如:
Your question isn't detailed enough, but in general you'll want to set classes on the specific li elements as needed.
For example:
有多种方法可以访问特定的元素索引。
:eq()
选择器可以访问单个索引:http:// api.jquery.com/eq-selector/
.nextAll()
函数返回匹配元素集中每个元素的所有后续同级元素:http://api.jquery.com/nextAll/
.prevAll()
的工作方式相同,但返回之前的兄弟:http://api.jquery.com/prevAll/
There are several ways to access a specific index of elements.
The
:eq()
selector can access a single index:http://api.jquery.com/eq-selector/
The
.nextAll()
function returns all following siblings of each element in the set of matched elements:http://api.jquery.com/nextAll/
The
.prevAll()
works the same way but returns the previous siblings:http://api.jquery.com/prevAll/
您可以使用 .filter() 方法来过滤您的选择。
现在,任何满足此条件的东西都将获得类属性。在您的示例中,第 3、第 4 和第 5 里将收到它。
you can use
.filter()
method to filter down your selection.Now, anything meets this criteria, will receive the class attribute. In your example, 3rd, 4th and 5th li will receive it.