效果
1.html:
<div class="aaa">11</div>
<div class="aaa">11</div>
<div class="aaa">11</div>
<div class="aaa">11</div>
2.css
.aaa0{
width: 100px;
height: 100px;
background-color: green;
}
.aaa1{
width: 100px;
height: 100px;
background-color: red;
}
.aaa2{
width: 100px;
height: 100px;
background-color: blue;
}
3.js
<script>
var list = document.getElementsByTagName("div") //获取div元素
var rel = [];
for (var i = 0; i < list.length; i++)
{
if (list[i].tagName === 'DIV') {
//list[i].setAttribute("class", "'aaa' + [i]");
list[i].className = 'aaa'+[i]; //修改class名,
rel.push(list[i]);
}
}
console.log(rel);
</script>