如何给连续的CSS类赋予不同的颜色(Javascript)?

如何给连续的CSS类赋予不同的颜色(Javascript)?,javascript,css,Javascript,Css,我有带注释的文本,我希望某些带注释的单词与它们的注释一起进行颜色编码,但我不希望必须手动进行。有没有办法让javascript(或jquery)甚至css将第一个class=“Noted”变为绿色,然后将第二个变为蓝色,然后在第五个变为绿色,并对相应的class=“note”s执行相同的操作 是的,可以使用。您可以在匹配事件列表中获取第一个、第二个、第三个等元素。是的,可以使用。您可以在匹配事件列表中获取第一个、第二个、第三个等元素。您可以使用getElementsByClass获取所有元素,然

我有带注释的文本,我希望某些带注释的单词与它们的注释一起进行颜色编码,但我不希望必须手动进行。有没有办法让javascript(或jquery)甚至css将第一个
class=“Noted”
变为绿色,然后将第二个变为蓝色,然后在第五个变为绿色,并对相应的
class=“note”
s执行相同的操作

是的,可以使用。您可以在匹配事件列表中获取第一个、第二个、第三个等元素。

是的,可以使用。您可以在匹配事件列表中获取第一个、第二个、第三个等元素。

您可以使用getElementsByClass获取所有元素,然后简单地遍历它们,给出每个元素及其对应的元素class=“note”不同的颜色。

您可以使用getElementsByClass获取所有元素,然后简单地遍历它们,为每个元素及其对应的元素class=“note”提供不同的颜色。

您可以使用:n子元素来实现这一点,但您需要类似jQuery的东西来支持IE。。正在做那件事

这里是仅CSS版本的第一个提琴版本**fiddle在下面用新代码更新**

CSS:


最终更新使用第三个更新的代码,并包括一些IE的jQuery-

您可以使用:n个孩子来完成此操作,但您需要类似jQuery的东西来支持IE。。正在做那件事

这里是仅CSS版本的第一个提琴版本**fiddle在下面用新代码更新**

CSS:


最终更新使用第三次更新的代码,并在jQuery中包含一些IE的jQuery-

…根据需要设置颜色


$(“.Noted”)。每个(功能(即,e){
交换机(i%4){
案例0:$(this.css({color:#f00});break;
案例1:$(this.css({color:#0f0“});break;
案例2:$(this.css({color:#00f“});break;
案例3:$(this.css({color:#ff9});break;
案例4:$(this.css({color:#f90});break;
}
});

在jquery中……根据您的需要设置颜色


$(“.Noted”)。每个(功能(即,e){
交换机(i%4){
案例0:$(this.css({color:#f00});break;
案例1:$(this.css({color:#0f0“});break;
案例2:$(this.css({color:#00f“});break;
案例3:$(this.css({color:#ff9});break;
案例4:$(this.css({color:#f90});break;
}
});
给你:

<html>
 <head>
 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>

  <title>Cycle classes</title>

<style>
.blue {

    background-color: blue;
}

.green {

    background-color: green;
}

.yellow {

    background-color: yellow;
}
</style>

<script>


$(document).ready( function() {
$(".Noted").each(function(i) {
    var classes = ['green','blue','yellow'];
    $(this).addClass(classes[i % classes.length])
});

})

 </script>



</head>

<body>

 <div class="Noted">hello</div>
 <div class="Noted">world</div>
 <div class="Noted">it works</div>
 </body>
</html>

循环类
蓝先生{
背景颜色:蓝色;
}
格林先生{
背景颜色:绿色;
}
黄先生{
背景颜色:黄色;
}
$(文档).ready(函数(){
$(“.Noted”)。每个(功能(i){
变量类=['绿色','蓝色','黄色'];
$(this.addClass(类[i%classes.length])
});
})
你好
世界
它起作用了
给你:

<html>
 <head>
 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>

  <title>Cycle classes</title>

<style>
.blue {

    background-color: blue;
}

.green {

    background-color: green;
}

.yellow {

    background-color: yellow;
}
</style>

<script>


$(document).ready( function() {
$(".Noted").each(function(i) {
    var classes = ['green','blue','yellow'];
    $(this).addClass(classes[i % classes.length])
});

})

 </script>



</head>

<body>

 <div class="Noted">hello</div>
 <div class="Noted">world</div>
 <div class="Noted">it works</div>
 </body>
</html>

循环类
蓝先生{
背景颜色:蓝色;
}
格林先生{
背景颜色:绿色;
}
黄先生{
背景颜色:黄色;
}
$(文档).ready(函数(){
$(“.Noted”)。每个(功能(i){
变量类=['绿色','蓝色','黄色'];
$(this.addClass(类[i%classes.length])
});
})
你好
世界
它起作用了

首先,尝试将元素封装在容器中。这将使孩子们的选择更加容易

<div id="parent">
     <span class="note">Green</span>, <span class="note">blue</span> 
     then <span class="note">red</span>.
</div>

绿色,蓝色
然后是红色。
然后,js:

<script>
var  children = document.getElementById('parent').getElementsByTagName('*')
    ,colours = ['green','blue','red','orange']
    ,i,j=0,max;

for (i = 0, max = children.length; i<max; i++) {
    if(children[i].getAttribute('class') == 'note') {
       children[i].setAttribute('style','color:' + colours[j]);
       j++;
       if (j>colours.length) {
          j = 0;
       }
    }
}
</script>

var children=document.getElementById('parent').getElementsByTagName('*'))
,颜色=[‘绿色’、‘蓝色’、‘红色’、‘橙色’]
,i,j=0,最大值;
对于(i=0,max=children.length;icolours.length){
j=0;
}
}
}

首先,尝试将元素封装在容器中。这将使孩子们的选择更加容易

<div id="parent">
     <span class="note">Green</span>, <span class="note">blue</span> 
     then <span class="note">red</span>.
</div>

绿色,蓝色
然后是红色。
然后,js:

<script>
var  children = document.getElementById('parent').getElementsByTagName('*')
    ,colours = ['green','blue','red','orange']
    ,i,j=0,max;

for (i = 0, max = children.length; i<max; i++) {
    if(children[i].getAttribute('class') == 'note') {
       children[i].setAttribute('style','color:' + colours[j]);
       j++;
       if (j>colours.length) {
          j = 0;
       }
    }
}
</script>

var children=document.getElementById('parent').getElementsByTagName('*'))
,颜色=[‘绿色’、‘蓝色’、‘红色’、‘橙色’]
,i,j=0,最大值;
对于(i=0,max=children.length;icolours.length){
j=0;
}
}
}

如果HTML是由服务器端脚本生成的,您可以让脚本指定一个类,根据该类生成注释,然后在样式表中为该类指定一种颜色,如下所示:

.note1 { //Corresponds to class='note1'
  color: green; //or whatever you want
}
.note2 { //Corresponds to class='note2'
  color: blue; //or whatever you want
}
/* and so on */
如果HTML只是静态编写的,那么根据您想要的颜色,为其在样式表中定义的方式分配相应的类

如果他们是孩子,你可以用克莱尔苏兹的解决方案

另一个选项是将它们都指定为class
note
,然后使用javascript根据您设置的预定义顺序将所有标记为class
note
的内容着色

这可能与以下内容类似(使用jQuery):

此处演示:


如果HTML是由服务器端脚本生成的,您可以让脚本指定一个类,根据该类生成注释,然后在样式表中为该类指定一种颜色,如下所示:

.note1 { //Corresponds to class='note1'
  color: green; //or whatever you want
}
.note2 { //Corresponds to class='note2'
  color: blue; //or whatever you want
}
/* and so on */
如果HTML只是静态编写的,那么根据您想要的颜色,为其在样式表中定义的方式分配相应的类

如果他们是孩子,你可以用克莱尔苏兹的解决方案

另一个选项是将它们都指定为class
note
,然后使用javascript根据您设置的预定义顺序将所有标记为class
note
的内容着色

这可能与以下内容类似(使用jQuery):

此处演示:


好的,根据您的JSFIDLE,您可以使用以下方法来获得您想要的结果:

p:nth-child(5n+1) .Noted, p:nth-child(5n+1) .Annotation {color: green}

Ok中所示,基于您的JSFIDLE,您可以使用以下内容来获得您想要的结果:

p:nth-child(5n+1) .Noted, p:nth-child(5n+1) .Annotation {color: green}

中所示,内联元件上的“注释”类