<style>
.color1 { color:red; }
.bgcolor1 { background-color:lightyellow; }
.bgcolor2 { background-color:lightgray; }
.bgcolor3 { background-color:lightgreen; }
.bold { font-weight:bold; }
.italic { font-style:italic; }
</style>
<div id="id1">
<ul>
<li class="class1">class1
<li class="class2">class2
<li class="class3">class3
<li class="class4">class4
<li class="class1">class1
<li class="class2">class2
<li class="class3">class3
<li class="class4">class4
<li class="class1" id="id9">id9 class1
</ul>
</div>
<script>
$( "#id1 .class1" ).addClass("color1").append(" (color1)");
$( "#id9" ).addClass("bold italic").append(" (bold italic)");
$( "#id1 li" ).addClass( function(index, currentClassName) {
var className;
switch (index % 3) {
case 0:
className = "bgcolor1";
break;
case 1:
className = "bgcolor2";
break;
case 2:
className = "bgcolor3";
break;
}
$( this ).append(" [" + index + ":" + currentClassName + " " + className + "]");
return className;
});
</script>
- class1
- class2
- class3
- class4
- class1
- class2
- class3
- class4
- id9 class1