<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; } table { width: 500px; margin: 20px auto; border: 1px solid #333; border-collapse: collapse; } table td, table th { border: 1px solid #222222; padding: 20px; text-align: center; } </style> </head> <body> <table> <caption>学生信息表</caption> <thead> <tr> <td>姓名</td> <td>年龄</td> <td>学号</td> <td>年纪</td> <td>操作</td> </tr> </thead> <tbody> <tr> <td>3</td> <td>3</td> <td>3</td> <td>3</td> <td>3</td> </tr> <tr> <td>3</td> <td>3</td> <td>3</td> <td>3</td> <td>3</td> </tr> <tr> <td>3</td> <td>3</td> <td>3</td> <td>3</td> <td>3</td> </tr> <tr> <td>3</td> <td>3</td> <td>3</td> <td>3</td> <td>3</td> </tr> <tr> <td>3</td> <td>3</td> <td>3</td> <td>3</td> <td>3</td> </tr> <tr> <td>3</td> <td>3</td> <td>3</td> <td>3</td> <td>3</td> </tr> </tbody> </table> <script> var trs = document.querySelectorAll('tbody tr'); for (var i = 0; i < trs.length; i++) { if (i % 2 == 0) { // 偶数 trs[i].style.backgroundColor = 'pink' } else { trs[i].style.backgroundColor = 'skyblue' } var bg; trs[i].onmouseover = function () { bg = this.style.backgroundColor; this.style.backgroundColor = 'yellow' } trs[i].onmouseout = function () { this.style.backgroundColor = bg } } </script> </body> </html>