表格隔行變色+高亮顯示

1.效果圖


2.代碼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        window.onload = function () {
            var oTab = document.getElementById('student');
            var oRow = oTab.tBodies[0].rows;
            var oOldColor;
            for (var i = 0; i < oRow.length; i++) {
                if (i % 2 == 0) {
                    oRow[i].style.background = 'pink';
                } else {
                    oRow[i].style.background = '';
                }
                oRow[i].onmouseover = function () {
                    oOldColor = this.style.backgroundColor;
                    this.style.background = 'green'
                }
                oRow[i].onmouseout = function () {
                    this.style.background = oOldColor;
                }
            }
        }
    </script>
    <style>
        table {
            width: 300px;
            border-collapse: collapse;
        }

        table td {
            border: 1px solid;
            text-align: center;
        }
    </style>
</head>

<body>
    <table id="student">
        <thead>
        <tr>
            <th>ID</th>
            <th>姓名</th>
            <th>年齡</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>1</td>
            <td>張三</td>
            <td>10</td>
        </tr>
        <tr>
            <td>2</td>
            <td>李四</td>
            <td>12</td>
        </tr>
        <tr>
            <td>3</td>
            <td>王五</td>
            <td>40</td>
        </tr>
        <tr>
            <td>4</td>
            <td>趙六</td>
            <td>30</td>
        </tr>
        <tr>
            <td>5</td>
            <td>閻七</td>
            <td>25</td>
        </tr>
        </tbody>
    </table>
</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章