homework04.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. #data {
  8. border-collapse: collapse;
  9. }
  10. #data td, #data th {
  11. width: 120px;
  12. height: 40px;
  13. text-align: center;
  14. border: 1px solid black;
  15. }
  16. #buttons {
  17. margin: 10px 0;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <table id="data">
  23. <caption>数据统计表</caption>
  24. <tbody>
  25. <tr>
  26. <th>姓名</th>
  27. <th>年龄</th>
  28. <th>性别</th>
  29. <th>身高</th>
  30. <th>体重</th>
  31. </tr>
  32. <tr>
  33. <td>Item1</td>
  34. <td>Item2</td>
  35. <td>Item3</td>
  36. <td>Item4</td>
  37. <td>Item5</td>
  38. </tr>
  39. <tr>
  40. <td>Item1</td>
  41. <td>Item2</td>
  42. <td>Item3</td>
  43. <td>Item4</td>
  44. <td>Item5</td>
  45. </tr>
  46. <tr>
  47. <td>Item1</td>
  48. <td>Item2</td>
  49. <td>Item3</td>
  50. <td>Item4</td>
  51. <td>Item5</td>
  52. </tr>
  53. <tr>
  54. <td>Item1</td>
  55. <td>Item2</td>
  56. <td>Item3</td>
  57. <td>Item4</td>
  58. <td>Item5</td>
  59. </tr>
  60. <tr>
  61. <td>Item1</td>
  62. <td>Item2</td>
  63. <td>Item3</td>
  64. <td>Item4</td>
  65. <td>Item5</td>
  66. </tr>
  67. <tr>
  68. <td>Item1</td>
  69. <td>Item2</td>
  70. <td>Item3</td>
  71. <td>Item4</td>
  72. <td>Item5</td>
  73. </tr>
  74. </tbody>
  75. </table>
  76. <div id="buttons">
  77. <button id="pretty">隔行换色</button>
  78. <button id="clear">清除数据</button>
  79. <button id="remove">删单元格</button>
  80. <button id="hide">隐藏表格</button>
  81. </div>
  82. <script src="js/mylib.js"></script>
  83. <script>
  84. function prettify() {
  85. var trs = document.querySelectorAll('#data tr');
  86. for (var i = 1; i < trs.length; i += 1) {
  87. trs[i].style.backgroundColor =
  88. i % 2 == 0 ? 'lightgray' : 'lightsteelblue';
  89. }
  90. }
  91. function clear() {
  92. var tds = document.querySelectorAll('#data td');
  93. for (var i = 0; i < tds.length; i += 1) {
  94. tds[i].textContent = '';
  95. }
  96. }
  97. function removeLastRow() {
  98. var table = document.getElementById('data');
  99. if (table.rows.length > 1) {
  100. table.deleteRow(table.rows.length - 1);
  101. }
  102. }
  103. function hideTable() {
  104. var table = document.getElementById('data');
  105. table.style.visibility = 'hidden';
  106. }
  107. +function() {
  108. var prettyBtn = document.querySelector('#pretty');
  109. prettyBtn.addEventListener('click', prettify)
  110. var clearBtn = document.querySelector('#clear');
  111. clearBtn.addEventListener('click', clear);
  112. var removeBtn = document.querySelector('#remove');
  113. removeBtn.addEventListener('click', removeLastRow);
  114. var hideBtn = document.querySelector('#hide');
  115. hideBtn.addEventListener('click', hideTable);
  116. }();
  117. </script>
  118. </body>
  119. </html>