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>表格</title>
    7. <style>
    8. .table-wrapper{
    9. width: 500px;
    10. margin: 0 auto;
    11. }
    12. table{
    13. width: 500px;
    14. height: 400px;
    15. text-align: center;
    16. }
    17. .odd{
    18. background-color: pink;
    19. }
    20. .even{
    21. background-color: cornflowerblue;
    22. }
    23. .hight-light{
    24. background-color: salmon;
    25. }
    26. </style>
    27. </head>
    28. <body>
    29. <div class="table-wrapper">
    30. </div>
    31. <script>
    32. //页面加载完毕后执行函数
    33. window.onload=function(){
    34. init();
    35. handleSelect();
    36. }
    37. function init(){
    38. let users=[
    39. {
    40. id:1,
    41. name:'xl',
    42. age:22
    43. },
    44. {
    45. id:2,
    46. name:'xw',
    47. age:23
    48. },
    49. {
    50. id:3,
    51. name:'xb',
    52. age:24
    53. },
    54. {
    55. id:4,
    56. name:'xl',
    57. age:22
    58. },
    59. {
    60. id:5,
    61. name:'xw',
    62. age:23
    63. },
    64. {
    65. id:6,
    66. name:'xb',
    67. age:24
    68. }
    69. ]
    70. //使用js创建表格
    71. let wrapper = document.querySelector('.table-wrapper')
    72. let innerHTML='<table>';
    73. innerHTML+='<thead>';
    74. innerHTML+='<tr><th><input type="checkbox"></th><th>序号</th><th>姓名</th><th>年龄</th></tr>';
    75. innerHTML+='</thead>';
    76. innerHTML+='<tbody>';
    77. for (let user of users) {
    78. innerHTML+='<tr>'
    79. innerHTML+='<td><input type="checkbox" value=""></td>';
    80. innerHTML+='<td>'+user.id+'</td>';
    81. innerHTML+='<td>'+user.name+'</td>';
    82. innerHTML+='<td>'+user.age+'</td>';
    83. innerHTML+='</tr>'
    84. }
    85. innerHTML+='</tbody>';
    86. innerHTML+='</table>';
    87. //将字符串放进去
    88. wrapper.innerHTML=innerHTML;
    89. //通过js设置所有行的背景
    90. let rows = wrapper.querySelectorAll('tbody tr')
    91. //通过style设置样式
    92. // for(let index in rows){
    93. // //判断奇偶数行
    94. // if(index%2==0){
    95. // rows[index].style.backgroundColor = 'pink';
    96. // }else{
    97. // rows[index].style.backgroundColor = ' cornflowerblue';
    98. // }
    99. // }
    100. //通过class设置样式
    101. for (let index in rows) {
    102. if(index%2==0){
    103. // rows[index].className='odd'
    104. rows[index].classList.add('odd')
    105. }else{
    106. rows[index].className=' even'
    107. }
    108. //鼠标在上面的时候添加样式
    109. rows[index].onmouseover=function(){
    110. this.classList.add('hight-light')
    111. }
    112. //离开的时候删除样式
    113. rows[index].onmouseout=function(){
    114. this.classList.remove('hight-light')
    115. }
    116. }
    117. }
    118. //实现复选框全选或全不选
    119. function handleSelect(){
    120. //添加事件监听
    121. document.querySelector("thead [type='checkbox']").addEventListener('click',function(){
    122. let list = document.querySelectorAll("tbody [type='checkbox']");
    123. for (let ck of list) {
    124. // ck.checked = !ck.checked;
    125. ck.checked = this.checked;
    126. }
    127. })
    128. }
    129. </script>
    130. </body>
    131. </html>