js实现表格隔行换色

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .table-wrapper {
  9. width: 500px;
  10. margin: auto auto;
  11. }
  12. table{
  13. width: 500px;
  14. height: 500px;
  15. text-align: center;
  16. }
  17. /* 奇数行 */
  18. .odd{
  19. background-color: pink;
  20. }
  21. /* 偶数行 */
  22. .even{
  23. background-color: royalblue;
  24. }
  25. .odd-high-light{
  26. background-color: yellow;
  27. }
  28. .even-high-light{
  29. background-color: white;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div class="table-wrapper">
  35. </div>
  36. <script>
  37. // 页面加载完毕后执行函数
  38. window.onload = function () {
  39. let users=[
  40. {
  41. id:1,
  42. name:"Lynn",
  43. age:18
  44. },
  45. {
  46. id:2,
  47. name:"Lynn",
  48. age:18
  49. },
  50. {
  51. id:3,
  52. name:"Lynn",
  53. age:18
  54. },
  55. {
  56. id:4,
  57. name:"Lynn",
  58. age:18
  59. }
  60. ]
  61. //使用js创建表格
  62. let wrapper = document.querySelector('.table-wrapper');
  63. let innerHTML = '<table>';
  64. innerHTML += '<thead>';
  65. innerHTML += '<tr><th>序号</th><th>姓名</th><th>年龄</th></tr>';
  66. innerHTML += '</thead>';
  67. innerHTML += '<tbody>';
  68. for(let user of users){
  69. innerHTML += '<tr>';
  70. innerHTML += '<td>'+user.id+'</td>';
  71. innerHTML += '<td>'+user.name+'</td>';
  72. innerHTML += '<td>'+user.age+'</td>';
  73. innerHTML += '</tr>';
  74. }
  75. innerHTML += '</tbody>';
  76. innerHTML += '</table>';
  77. wrapper.innerHTML=innerHTML;//页面已经更新了
  78. // 通过js设置行的背景色
  79. let rows=wrapper.querySelectorAll('tbody tr');
  80. //通过style设置样式
  81. /* for(let index in rows){
  82. if(index%2==0){
  83. rows[index].style.backgroundColor='pink';
  84. }else{
  85. rows[index].style.backgroundColor='green';
  86. }
  87. } */
  88. //通过class设置样式
  89. for(let index in rows){
  90. if(index%2==0){
  91. // className:有多个名字会覆盖样式
  92. // classList:不会覆盖
  93. rows[index].classList.add('odd');
  94. rows[index].onmouseover=function(){
  95. // console.log(this);//this表示当前行
  96. this.classList.add('odd-high-light');
  97. }
  98. rows[index].onmouseout=function(){
  99. // console.log(this);//this表示当前行
  100. this.classList.remove('odd-high-light');
  101. }
  102. }else{
  103. rows[index].className='even';
  104. rows[index].onmouseover=function(){
  105. // console.log(this);//this表示当前行
  106. this.classList.add('even-high-light');
  107. }
  108. rows[index].onmouseout=function(){
  109. // console.log(this);//this表示当前行
  110. this.classList.remove('even-high-light');
  111. }
  112. }
  113. /* // 高亮,鼠标在上面添加高亮样式,鼠标移开删除高亮样式
  114. rows[index].onmouseover=function(){
  115. // console.log(this);//this表示当前行
  116. this.classList.add('odd-high-light');
  117. }
  118. rows[index].onmouseout=function(){
  119. // console.log(this);//this表示当前行
  120. this.classList.remove('odd-high-light');
  121. } */
  122. }
  123. }
  124. </script>
  125. </body>
  126. </html>