Discuz教程网

Table隔行变色的JavaScript代码

[复制链接]
authicon 09927306 发表于 2011-1-18 13:51:13 | 显示全部楼层 |阅读模式
用js实现的table隔行变色,鼠标放上去有变色显示,需要的朋友可以参考下。
核心代码:
代码如下:


  1. <table id="datatable">
  2. <tr>
  3. <td>1314学习网</td>
  4. </tr>
  5. <tr>
  6. <td>www.discuz.1314study.com</td>
  7. </tr>
  8. <tr>
  9. <td>1314学习网</td>
  10. </tr>
  11. <tr>
  12. <td>www.discuz.1314study.com</td>
  13. </tr>
  14. <tr>
  15. <td>1314学习网</td>
  16. </tr>
  17. <tr>
  18. <td>www.discuz.1314study.com</td>
  19. </tr>
  20. <tr>
  21. <td>1314学习网</td>
  22. </tr>
  23. <tr>
  24. <td>www.discuz.1314study.com</td>
  25. </tr>
  26. </table>
  27. <script>
  28. function showtable(){
  29. var color1 = "rgb(234,240,255)";
  30. var color2 = "rgb(255,255,255)";
  31. var bgColor = "rgb(255,255,193)";
  32. var trs = document.getElementById("datatable").getElementsByTagName("tr");
  33. for (var i=0;i<trs.length-1;i++){
  34. if (i%2==0) {
  35. trs[i].style.backgroundColor=color1;
  36. trs[i].onmouseover = function(){
  37. this.style.backgroundColor = bgColor;
  38. }
  39. trs[i].onmouseout = function(){
  40. this.style.backgroundColor = color1;
  41. }
  42. } else {
  43. trs[i].style.backgroundColor=color2;
  44. trs[i].onmouseover = function(){
  45. this.style.backgroundColor = bgColor;
  46. }
  47. trs[i].onmouseout = function(){
  48. this.style.backgroundColor = color2;
  49. }
  50. }
  51. }
  52. }
  53. showtable()
  54. </script>
复制代码






上一篇:javascript 内存回收机制理解
下一篇:JavaScript版本迷局介绍
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 18:23

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表