日期:2014-05-16 浏览次数:20406 次
html>
<title>
<title>Mypage</title>
<script type="text/javascript" src="temp.js"></script>
<link rel="stylesheet" href="temp.css" type="text/css"/>
<body>
<div id="box">
<div id="box1"></div>
<div id="box2"></div>
<div id="box3"></div>
<div id="box4"></div>
<div id="box5"></div>
<div id="box6"></div>
</div>
</body>
</html>
window.onload = initAll;
function initAll() {
var id = document.getElementById("box");
var littleBoxs = id.childNodes;
var j = 0;
for(var i = 0;i <littleBoxs.length;i++){
if(littleBoxs[i].id != null){
if(parseInt(littleBoxs[i].id.substr(3,1)) % 2 == 0){
littleBoxs[i].className = "classRed";
} else {
littleBoxs[i].className = "classYellow";
}
}
}
}
#box {
height:100px;
width:600px;
}
.classRed {
background-color:red;
display:inline;
width:100px;
height:100px;
}
.classYellow {
background-color:yellow;
display:inline;
width:100px;
height:10px;
}
<div id="box">
<div id="box1">111</div>
<div id="box2">222</div>
<div id="box3">333</div>
<div id="box4">444</div>
<div id="box5">555</div>
<div id="box6">666</div>
</div>