example09.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. <!-- jQuery: Write Less Do More -->
  83. <!-- 加载本地的jQuery文件适合开发和测试时使用 -->
  84. <script src="js/jquery.min.js"></script>
  85. <!-- 下面的方式适合商业项目通过CDN服务器来加速获取jQuery的JS文件 -->
  86. <!-- <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> -->
  87. <script>
  88. // JavaScript Object Notation == JSON
  89. var stu = {
  90. 'id': 1001,
  91. 'name': '骆昊',
  92. 'age': 15,
  93. 'study': function(course) {
  94. alert(this.name + '正在学习' + course);
  95. },
  96. 'watchAv': function() {
  97. if (this.age >= 18) {
  98. alert(this.name + '正在观看岛国动作片');
  99. } else {
  100. alert(this.name + '只能观看《熊出没》');
  101. }
  102. }
  103. };
  104. stu.study('Python');
  105. stu.watchAv();
  106. $(function() {
  107. $('#hide').on('click', function() {
  108. // 根据样式表选择器获取元素 获取到的不是原生的JS对象
  109. // 而是经过jQuery封装过后的对象(有更多的方法方便操作)
  110. $('#data').fadeOut(2000);
  111. });
  112. $('#remove').on('click', function() {
  113. $('#data tr:gt(0):last-child').remove();
  114. });
  115. $('#clear').on('click', function() {
  116. $('#data tr:gt(0)>td').empty();
  117. });
  118. $('#pretty').on('click', function() {
  119. $('#data tr:gt(0):odd').css({
  120. 'background-color': '#ccc',
  121. 'font-size': '36px',
  122. 'font-weight': 'bolder'
  123. });
  124. $('#data tr:gt(0):even').css('background-color', '#abc');
  125. });
  126. });
  127. </script>
  128. </body>
  129. </html>