js表格变色制作表格隔行变色,滑过选中变色特效

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Table with Row Coloring Effects</title> <style> body { font-family: Arial, sans-serif; margin: 0; padding: 20px; background-color: #f5f5f5; } .container { max-width: 800px; margin: 0 auto; background-color: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); } h1 { color: #333; text-align: center; margin-bottom: 30px; } table { width: 100%; border-collapse: collapse; margin-bottom: 20px; } th { background-color: #4CAF50; color: white; padding: 12px; text-align: left; } td { padding: 10px; border-bottom: 1px solid #ddd; } tr:nth-child(even) { background-color: #f2f2f2; } tr:hover { background-color: #e6f7ff; cursor: pointer; } tr.selected { background-color: #b3e0ff; font-weight: bold; } .footer { text-align: center; margin-top: 30px; font-size: 14px; color: #666; } a { color: #4CAF50; text-decoration: none; } a:hover { text-decoration: underline; } </style> </head> <body> <div class="container"> <h1>Interactive Table with Coloring Effects</h1> <table id="dataTable"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Email</th> <th>Department</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>John Smith</td> <td>john@example.com</td> <td>Marketing</td> </tr> <tr> <td>2</td> <td>Sarah Johnson</td> <td>sarah@example.com</td> <td>Engineering</td> </tr> <tr> <td>3</td> <td>Michael Brown</td> <td>michael@example.com</td> <td>Sales</td> </tr> <tr> <td>4</td> <td>Emily Davis</td> <td>emily@example.com</td> <td>Human Resources</td> </tr> <tr> <td>5</td> <td>Robert Wilson</td> <td>robert@example.com</td> <td>Finance</td> </tr> <tr> <td>6</td> <td>Jennifer Lee</td> <td>jennifer@example.com</td> <td>Operations</td> </tr> </tbody> </table> <div class="footer"> Check out our partner site: </div> </div> <script> document.addEventListener('DOMContentLoaded', function() { const table = document.getElementById('dataTable'); const rows = table.getElementsByTagName('tr'); // Add click event to select rows for (let i = 1; i < rows.length; i++) { // Start from 1 to skip header rows[i].addEventListener('click', function() { // Remove selected class from all rows for (let j = 1; j < rows.length; j++) { rows[j].classList.remove('selected'); } // Add selected class to clicked row this.classList.add('selected'); }); } // Alternate row coloring is handled by CSS nth-child selector // Hover effect is handled by CSS :hover selector }); </script> </body> </html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值