如何在每个第1,第4,第5,第8,第9,第12,第13日添加课程

时间:2017-08-08 08:47:33

标签: javascript jquery

我需要通过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');
    }
});

4 个答案:

答案 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(在上文中进行了修改)以获取示例。

以下是结果图:

list of elements with highlighting in line with Origina lposters requirements

答案 3 :(得分:0)

好的,我在这里解决的是模式:

$('.touch').each(function(index) {

    if ( index % 4 == 0 || (index + 1) % 4 == 0 ) {

        $(this).addClass('touched');
    }
});

https://codepen.io/matt3224/pen/EvWbGe?editors=1010