我通过一个最简单的例子来测试,结果发现我还是喜欢用jq来实现,呵呵。虽然很简单,但是还记下来了,学习的过程,哈哈。。。
html文件:
<html>
<head>
<title>隔行换色</title>
</head>
<body>
<ul>
<li>111111</li>
<li>222222</li>
<li>333333</li>
<li>444444</li>
</ul>
</body>
</html>
<head>
<title>隔行换色</title>
</head>
<body>
<ul>
<li>111111</li>
<li>222222</li>
<li>333333</li>
<li>444444</li>
</ul>
</body>
</html>
第一种方法,通过jQuery来实现(别忘记导入jQuery.js哦):
<script>
$(document).ready(function(){
$("li:odd").css("background","#9FB7F6");
$("li:even").css("background","#B6C8F8");
});
</script>
$(document).ready(function(){
$("li:odd").css("background","#9FB7F6");
$("li:even").css("background","#B6C8F8");
});
</script>
第二种方法,通过css实现
<style type="text/css">
UL.myul1 LI{background-color: expression(this.sourceIndex%2==0?'#9FB7F6':'#B6C8F8');
}
</style>
UL.myul1 LI{background-color: expression(this.sourceIndex%2==0?'#9FB7F6':'#B6C8F8');
}
</style>
第三种方法,通过CSS和JS实现(注:此处JS尽量加在ul后面,不要加在<head>里面。当然也可以用window.onload。)
<style type="text/css">
.li01{background:#9FB7F6;}
.li02{background:#B6C8F8;}
</style>
.li01{background:#9FB7F6;}
.li02{background:#B6C8F8;}
</style>
<script language="JavaScript">
objName=document.getElementsByTagName("li")
objName=document.getElementsByTagName("li")
for (i=0;i<objName.length;i++) {
(i%2==0)?(objName(i).className = "li01"):(objName(i).className = "li02"); }
</script>
(i%2==0)?(objName(i).className = "li01"):(objName(i).className = "li02"); }
</script>