在实际开发当中经常遇到隔行换色特效,实际上原理就是定义奇数行和偶数行的各自颜色,然后用JS做一个循环判断各自的颜色,读取出来,实现的代码如下
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 5 <title>Ul li列表隔行换色</title> 6 </head><body> 7 <style> 8 .mytable {border-collapse:collapse;border:solid #6AA70B;border-width:0px 0 0 0px;width:50%;} 9 .mytable ul li {padding-top:5px;text-indent:2em;list-style:none;background:url() 3px 50% no-repeat;border-bottom:#ccc 1px dotted ;font-family: "Verdana";font-size: 9pt;color:#0066cc;text-align:left;height:25px;} 10 .mytable ul li.t1 {background-color:#EEE;}/* 第一行背景色 */ 11 .mytable ul li.t2{background-color:#FFF;}/* 第二行背景色 */ 12 .mytable ul li.t3 {background-color:skyblue;}/* 鼠标经过时的背景色 */ 13 </style> 14 <body style=margin:0;><br><br><br> 15 <div align="center"> 16 <div class=mytable id=tab> 17 <ul> 18 <li><a href="/jscss/code/62.shtml" target="_blank">CSS高度自适应代码(用了都说好)</a></li><li><a href="/jscss/code/105.shtml" target="_blank">一个很牛的登录窗口特效</a></li><li><a href="/jscss/code/138.shtml" target="_blank">用纯JS实现iFrame框架分栏显示效果</a></li><li><a href="/jscss/code/137.shtml" target="_blank">网页弹出层的JS代码</a></li><li><a href="http://www.codefans.net/jscss/code/150.shtml" target="_blank">DIV层拖动,代码很简洁</a></li><li><a href="/jscss/code/126.shtml" target="_blank">很流行的CSS+JS用户列表特效</a></li><li><a href="http://www.codefans.net/jscss/code/184.shtml" target="_blank">鼠标移到链接文字弹出一个提示层特效代码</a></li><li><a href="/jscss/code/149.shtml" target="_blank">CSS滤镜实现半透明层代码</a></li><li><a href="/jscss/code/170.shtml" target="_blank">用CSS实现一段文字自动底部对齐并随页面滚动</a></li> 19 </ul></div><script type="text/javascript"> 20 <!-- 21 var Ptr=document.getElementById("tab").getElementsByTagName("li"); 22 function $() { 23 for (i=1;i<Ptr.length+1;i++) { 24 Ptr[i-1].className = (i%2>0)?"t1":"t2"; 25 } 26 } 27 window.onload=$; 28 for(var i=0;i<Ptr.length;i++) { 29 Ptr[i].onmouseover=function(){ 30 this.tmpClass=this.className; 31 this.className = "t3"; 32 }; 33 Ptr[i].onmouseout=function(){ 34 this.className=this.tmpClass; 35 }; 36 } 37 //--> 38 </script> 39 </body> 40 </html>