当鼠标落在表格某一行行,某一行变粉色 当离开恢复原色
<!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> table { 800px; margin: 100px; text-align: center; border-collapse: collapse; font-size: 14px; } thead tr { height: 20px; background-attachment: skyblue; } tbody tr { height: 30px; } tbody td { border-bottom: 1px; font-size: 12px; color: blue; } .bg { background-color: pink; } </style> </head> <body> <table> <thead> <tr> <th>代码</th> <th>名称</th> <th>最新公布净值</th> <th>累计净值</th> <th>前单位净值</th> <th>净增长率</th> </tr> </thead> <tbody> <tr> <td>112</td> <td>农银金穗3个月定期开放债券</td> <td>1.083</td> <td>1.08</td> <td>1.009</td> <td>+0.33%</td> </tr> <tr> <td>113</td> <td>农银金穗3个月定期开放债券</td> <td>1.083</td> <td>1.08</td> <td>1.009</td> <td>+0.33%</td> </tr> <tr> <td>114</td> <td>农银金穗3个月定期开放债券</td> <td>1.083</td> <td>1.08</td> <td>1.009</td> <td>+0.33%</td> </tr> <tr> <td>115</td> <td>农银金穗3个月定期开放债券</td> <td>1.083</td> <td>1.08</td> <td>1.009</td> <td>+0.33%</td> </tr> <tr> <td>116</td> <td>农银金穗3个月定期开放债券</td> <td>1.083</td> <td>1.08</td> <td>1.009</td> <td>+0.33%</td> </tr> </tbody> </table> <script> //1.获取元素 获取的是tbody里面所有的行 var trs = document.querySelector('tbody').querySelectorAll('tr'); //2.利用循环 绑定注册事件 for(var i = 0 ; i < trs.length ;i++) { //3.鼠标经过事件onmouseover trs[i].onmouseover = function() { console.log('绑定成功'); this.className = 'bg'; } trs[i].onmouseout = function() { this.className = ''; } } </script> </body> </html>