我需要通过js为这些元素添加一个类来改变共济会布局。
如何在不手动指定模式的情况下完成此操作?
由于
模式是:
1st = class
第二
第三
4 =班级
5 =班级
6
7
8日=班级
9日=班级
10
第11
12日=班级
13日=班级
等。
Codepen尝试:Filter Package
$('.touch').each(function(index) {
if ( index === 0 || index % 3 ) {
$(this).addClass('touched');
}
});
答案 0 :(得分:2)
这是纯CSS的另一种方式
.touch:first-of-type, .touch:nth-of-type(4n), .touch:nth-of-type(4n+1) {
color: red;
}
答案 1 :(得分:1)
$('.touch').each(function(index) {
if ( index % 4 == 0 || index % 4 == 3 ) {
$(this).addClass('touched');
}
});
答案 2 :(得分:1)
你不需要javascript。这是一个纯CSS解决方案,您可以使用nth-child
使用两个类。
.pattern li:nth-child(4n),
.pattern li:nth-child(4n+1) {
background: lightsteelblue;
}
请参阅此code pen(在上文中进行了修改)以获取示例。
以下是结果图:
答案 3 :(得分:0)
好的,我在这里解决的是模式:
$('.touch').each(function(index) {
if ( index % 4 == 0 || (index + 1) % 4 == 0 ) {
$(this).addClass('touched');
}
});