利用html css javascript实现表格隔行变色效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    *{
        padding: 0;
        margin: 0;
        border-spacing: 0ex;
    }
    body{
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
    }
    table{
        text-align: center;
    }
    thead{
        background-color: pink;
        color: #fff;
    }
    tr{
        border-bottom: 1px solid black;
    }
    td{
        padding: 5px;
    }
    .bg{
        background-color:pink;
        color: #fff;
    }
</style>
<body>
    <table>
    <thead>
        <th>代码</th>
        <th>名称</th>
        <th>最新公布净值</th>
        <th>累计净值</th>
        <th>前单位净值</th>
    </thead>
    <tbody>
        <tr>
            <td>003526</td>
            <td>农银金辉3个月定期开放债券</td>
            <td>1.075</td>
            <td>1.079</td>
            <td>1.074</td>
        </tr>
        <tr>
            <td>003526</td>
            <td>农银金辉3个月定期开放债券</td>
            <td>1.075</td>
            <td>1.079</td>
            <td>1.074</td>
        </tr>
        <tr>
            <td>003526</td>
            <td>农银金辉3个月定期开放债券</td>
            <td>1.075</td>
            <td>1.079</td>
            <td>1.074</td>
        </tr>
        <tr>
            <td>003526</td>
            <td>农银金辉3个月定期开放债券</td>
            <td>1.075</td>
            <td>1.079</td>
            <td>1.074</td>
        </tr>
        <tr>
            <td>003526</td>
            <td>农银金辉3个月定期开放债券</td>
            <td>1.075</td>
            <td>1.079</td>
            <td>1.074</td>
        </tr>
        <tr>
            <td>003526</td>
            <td>农银金辉3个月定期开放债券</td>
            <td>1.075</td>
            <td>1.079</td>
            <td>1.074</td>
        </tr>
    </tbody>
    </table>
</body>
<script>
    var trs=document.querySelector('tbody').querySelectorAll('tr');
    // 遍历每个伪数组中的元素
    for(var i=0;i<trs.length;i++){
        // 绑定事件 鼠标经过
        trs[i].onmouseover=function(){
            this.className='bg';
        }
        trs[i].onmouseout=function(){
            this.className='';
        }
    
    }
</script>
</html>

猜你喜欢

转载自blog.csdn.net/weiyuyang250/article/details/120712426