<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> #data { border-collapse: collapse; } #data td, #data th { width: 120px; height: 40px; text-align: center; border: 1px solid black; } #buttons { margin: 10px 0; } </style> </head> <body> <table id="data"> <caption>数据统计表</caption> <tbody> <tr> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>身高</th> <th>体重</th> </tr> <tr> <td>Item1</td> <td>Item2</td> <td>Item3</td> <td>Item4</td> <td>Item5</td> </tr> <tr> <td>Item1</td> <td>Item2</td> <td>Item3</td> <td>Item4</td> <td>Item5</td> </tr> <tr> <td>Item1</td> <td>Item2</td> <td>Item3</td> <td>Item4</td> <td>Item5</td> </tr> <tr> <td>Item1</td> <td>Item2</td> <td>Item3</td> <td>Item4</td> <td>Item5</td> </tr> <tr> <td>Item1</td> <td>Item2</td> <td>Item3</td> <td>Item4</td> <td>Item5</td> </tr> <tr> <td>Item1</td> <td>Item2</td> <td>Item3</td> <td>Item4</td> <td>Item5</td> </tr> </tbody> </table> <div id="buttons"> <button id="pretty">隔行换色</button> <button id="clear">清除数据</button> <button id="remove">删单元格</button> <button id="hide">隐藏表格</button> </div> <!-- jQuery: Write Less Do More --> <!-- 加载本地的jQuery文件适合开发和测试时使用 --> <script src="js/jquery.min.js"></script> <!-- 下面的方式适合商业项目通过CDN服务器来加速获取jQuery的JS文件 --> <!-- <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> --> <script> $(function() { $('#hide').on('click', function() { // 根据样式表选择器获取元素获取到的不是原生的JS对象 // 而是经过jQuery封装过后的对象(有更多的方法方便操作) $('#data').fadeOut(2000); }); $('#remove').on('click', function() { $('#data tr:gt(0):last-child').remove(); }); $('#clear').on('click', function() { $('#data tr:gt(0)>td').empty(); }); $('#pretty').on('click', function() { $('#data tr:gt(0):odd').css({ 'background-color': '#ccc', 'font-size': '36px', 'font-weight': 'bolder' }); $('#data tr:gt(0):even').css('background-color', '#abc'); }); }); </script> </body> </html>