example_of_jquery_1.html 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. <thead>
  25. <tr>
  26. <th>姓名</th>
  27. <th>年龄</th>
  28. <th>性别</th>
  29. <th>身高</th>
  30. <th>体重</th>
  31. </tr>
  32. </thead>
  33. <tbody>
  34. <tr>
  35. <td>Item1</td>
  36. <td>Item2</td>
  37. <td>Item3</td>
  38. <td>Item4</td>
  39. <td>Item5</td>
  40. </tr>
  41. <tr>
  42. <td>Item1</td>
  43. <td>Item2</td>
  44. <td>Item3</td>
  45. <td>Item4</td>
  46. <td>Item5</td>
  47. </tr>
  48. <tr>
  49. <td>Item1</td>
  50. <td>Item2</td>
  51. <td>Item3</td>
  52. <td>Item4</td>
  53. <td>Item5</td>
  54. </tr>
  55. <tr>
  56. <td>Item1</td>
  57. <td>Item2</td>
  58. <td>Item3</td>
  59. <td>Item4</td>
  60. <td>Item5</td>
  61. </tr>
  62. <tr>
  63. <td>Item1</td>
  64. <td>Item2</td>
  65. <td>Item3</td>
  66. <td>Item4</td>
  67. <td>Item5</td>
  68. </tr>
  69. <tr>
  70. <td>Item1</td>
  71. <td>Item2</td>
  72. <td>Item3</td>
  73. <td>Item4</td>
  74. <td>Item5</td>
  75. </tr>
  76. </tbody>
  77. </table>
  78. <div id="buttons">
  79. <button id="pretty">隔行换色</button>
  80. <button id="clear">清除数据</button>
  81. <button id="remove">删除一行</button>
  82. <button id="hide">表格淡出</button>
  83. </div>
  84. <!-- 引入jQuery -->
  85. <script src="js/jquery.min.js"></script>
  86. <!-- <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> -->
  87. <script>
  88. // $('选择器') --> jQuery对象 --> 封装了很多的方法
  89. $('#pretty').on('click', () => {
  90. $('#data>tbody>tr:odd').css('background-color', 'lightgray')
  91. $('#data>tbody>tr:even').css('background-color', 'lightgreen')
  92. })
  93. $('#clear').on('click', () => {
  94. $('#data>tbody>tr>td').empty()
  95. })
  96. $('#remove').on('click', () => {
  97. $('#data>tbody>tr:last-child').remove()
  98. })
  99. $('#hide').on('click', () => {
  100. let title = $('#hide').text()
  101. if (title == '表格淡出') {
  102. $('#data').fadeOut(1000, () => {
  103. $('#hide').text('表格淡入')
  104. })
  105. } else {
  106. $('#data').fadeIn(2000, () => {
  107. $('#hide').text('表格淡出')
  108. })
  109. }
  110. })
  111. </script>
  112. </body>
  113. </html>