重温table隔行换色

无事,尝试写下隔行换色,写着写着就感觉自己在复制别人的东西,可悲啊.

不过,也好要档练习吧.

[attach=71]

XML/HTML代码
  1. <style type="text/css">  
  2. *{font-size:12px;color:#fff;}   
  3. body{background-color:#633801;}   
  4. .caption{border-collapse:collapse;border:solid #ddd;border-width:1px 1px 0;width:500px;text-align:center;background-color:#BF6B00;}   
  5. .aied {border-collapse:collapse;border:1px solid #ddd;width:500px;}   
  6. .aied td {border:solid #ddd;border-width:0 1px 1px 0;}   
  7. .aied th {border:solid #ddd;border-width:1px;}   
  8. tr.bg1 td{background-color:#885A1F;}   
  9. tr.bg2 td {background-color:#986A2E;}   
  10. tr.bg3 td {background-color:#BF6B00;}   
  11. </style>  
  12.   
  13. <table class="caption">  
  14. <tr><td>类别</td><td>数量</td><td>质量</td><td>价格</td></tr>  
  15. </table>  
  16. <table id="aitable" class="aied">  
  17. <tr><th>美女</th><td>美人鱼</td><td>杨丞琳</td><td>小可爱</td></tr>  
  18. <tr><th>wow</th><td>小D</td><td>QS</td><td>法师</td></tr>  
  19. <tr><th>小说</th><td>侏仙</td><td>清心诀</td><td>寸芒</td></tr>  
  20. <tr><th>运动</th><td>乒乓球</td><td>足球</td><td>羽毛球</td></tr>  
  21. <tr><th>城市</th><td>中山</td><td>桂平</td><td>北京</td></tr>  
  22. </table>  
  23. <br />  
  24. <br />  
  25. <table class="caption">  
  26. <tr><td>类别</td><td>数量</td><td>质量</td><td>价格</td></tr>  
  27. </table>  
  28. <table id="table" class="aied">  
  29. <tr><th>美女</th><td>美人鱼</td><td>杨丞琳</td><td>小可爱</td></tr>  
  30. <tr><th>wow</th><td>小D</td><td>QS</td><td>法师</td></tr>  
  31. <tr><th>小说</th><td>侏仙</td><td>清心诀</td><td>寸芒</td></tr>  
  32. <tr><th>运动</th><td>乒乓球</td><td>足球</td><td>羽毛球</td></tr>  
  33. <tr><th>城市</th><td>中山</td><td>桂平</td><td>北京</td></tr>  
  34. </table>  
  35.   
  36. <script type="text/javascript">  
  37. /* 第一个改class */   
  38. var aide=document.getElementById("aitable").getElementsByTagName("tr");   
  39. function $() {   
  40.     for (i=1;i<aide.length+1;i++) {    
  41.     aide[i-1].className = (i%2>0)?"bg1":"bg2";    
  42.     }   
  43. }   
  44. window.onload=$;   
  45. for(var i=0;i<aide.length;i++) {   
  46.     aide[i].onmouseover=function(){   
  47.     thisthis.tmpClass=this.className;   
  48.     this.className = "bg3";       
  49.     };   
  50.     aide[i].onmouseout=function(){   
  51.     thisthis.className=this.tmpClass;   
  52.     };   
  53. }   
  54.   
  55. /* 第二个改style.backgroundColor */   
  56.   
  57. var table = document.getElementById("table");   
  58. var trs = table.getElementsByTagName("tr");   
  59. for (var i=0;i<trs.length;i++) {   
  60.     if (i % 2==0){   
  61.         trs[i].style.backgroundColor = "#885A1F";   
  62.         continue;   
  63.     }   
  64.     trs[i].style.backgroundColor = "#986A2E";   
  65. }   
  66. </script>  

Tags: web标准, 隔行换色

« 上一篇 | 下一篇 »

发表评论

注:网址请加上 http:// 若无,报错...

评论内容 (必填):