表格全选()

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>表格全选</title>
    <style>
        table {
            border: 1px solid;
            width: 500px;
            margin-left: 30%;
        }

        td, th {
            text-align: center;
            border: 1px solid;
        }

        div {
            margin-top: 10px;
            margin-left: 30%;
        }

        .out {
            background-color: white;
        }

        .over {
            background-color: pink;
        }
    </style>

    <script>
        /**
         * 分析:
         * 1.全选
         * 获取所有的checkbox
         * 遍历cb,设置每一个cb的状态为选中checked
         */

        //在页面加载完后绑定事件
        window.onload = function () {
            //给全选按钮绑定单机事件
            document.getElementById("selectAll").onclick = function () {
                //全选
                //获取所有的checkbox
                var cbs = document.getElementsByName("cb");
                //遍历
                for (var i = 0; i < cbs.length; i++) {
                    //设置每一个cb的状态为选中
                    cbs[i].checked = true;
                }
            };

            document.getElementById("unSelectAll").onclick = function () {
                //全不选
                //获取所有的checkbox
                var cbs = document.getElementsByName("cb");
                //遍历
                for (var i = 0; i < cbs.length; i++) {
                    //设置每一个cb的状态为不选中
                    cbs[i].checked = false;
                }
            };

            document.getElementById("selectRev").onclick = function () {
                //反选
                //获取所有的checkbox
                var cbs = document.getElementsByName("cb");
                //遍历
                for (var i = 0; i < cbs.length; i++) {
                    //设置每一个cb的状态为相反
                    cbs[i].checked = !cbs[i].checked;
                }
            };


            var trs = document.getElementsByTagName("tr");
            for (var i = 0; i < trs.length; i++) {
                //移道元素之上
                trs[i].onmouseover = function () {
                    this.classList = "over";
                };
                //移出
                trs[i].onmouseout = function () {
                    this.classList = "out";
                };
            }
        };


    </script>
</head>
<body>

<table>
    <caption>学生信息表</caption>
    <tr>
        <th><input id="firstCb" name="cb" type="checkbox"></th>
        <th>编号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>操作</th>
    </tr>

    <tr>
        <td><input name="cb" type="checkbox"></td>
        <td>1</td>
        <td>令狐冲</td>
        <td>男</td>
        <td><a href="javascript:void(0);">删除</a></td>
    </tr>

    <tr>
        <td><input name="cb" type="checkbox"></td>
        <td>2</td>
        <td>任我行</td>
        <td>男</td>
        <td><a href="javascript:void(0);">删除</a></td>
    </tr>

    <tr>
        <td><input name="cb" type="checkbox"></td>
        <td>3</td>
        <td>岳不群</td>
        <td>?</td>
        <td><a href="javascript:void(0);">删除</a></td>
    </tr>

</table>
<div>
    <input id="selectAll" type="button" value="全选">
    <input id="unSelectAll" type="button" value="全不选">
    <input id="selectRev" type="button" value="反选">
</div>
</body>
</html>
————————
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>表格全选</title>
    <style>
        table {
            border: 1px solid;
            width: 500px;
            margin-left: 30%;
        }

        td, th {
            text-align: center;
            border: 1px solid;
        }

        div {
            margin-top: 10px;
            margin-left: 30%;
        }

        .out {
            background-color: white;
        }

        .over {
            background-color: pink;
        }
    </style>

    <script>
        /**
         * 分析:
         * 1.全选
         * 获取所有的checkbox
         * 遍历cb,设置每一个cb的状态为选中checked
         */

        //在页面加载完后绑定事件
        window.onload = function () {
            //给全选按钮绑定单机事件
            document.getElementById("selectAll").onclick = function () {
                //全选
                //获取所有的checkbox
                var cbs = document.getElementsByName("cb");
                //遍历
                for (var i = 0; i < cbs.length; i++) {
                    //设置每一个cb的状态为选中
                    cbs[i].checked = true;
                }
            };

            document.getElementById("unSelectAll").onclick = function () {
                //全不选
                //获取所有的checkbox
                var cbs = document.getElementsByName("cb");
                //遍历
                for (var i = 0; i < cbs.length; i++) {
                    //设置每一个cb的状态为不选中
                    cbs[i].checked = false;
                }
            };

            document.getElementById("selectRev").onclick = function () {
                //反选
                //获取所有的checkbox
                var cbs = document.getElementsByName("cb");
                //遍历
                for (var i = 0; i < cbs.length; i++) {
                    //设置每一个cb的状态为相反
                    cbs[i].checked = !cbs[i].checked;
                }
            };


            var trs = document.getElementsByTagName("tr");
            for (var i = 0; i < trs.length; i++) {
                //移道元素之上
                trs[i].onmouseover = function () {
                    this.classList = "over";
                };
                //移出
                trs[i].onmouseout = function () {
                    this.classList = "out";
                };
            }
        };


    </script>
</head>
<body>

<table>
    <caption>学生信息表</caption>
    <tr>
        <th><input id="firstCb" name="cb" type="checkbox"></th>
        <th>编号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>操作</th>
    </tr>

    <tr>
        <td><input name="cb" type="checkbox"></td>
        <td>1</td>
        <td>令狐冲</td>
        <td>男</td>
        <td><a href="javascript:void(0);">删除</a></td>
    </tr>

    <tr>
        <td><input name="cb" type="checkbox"></td>
        <td>2</td>
        <td>任我行</td>
        <td>男</td>
        <td><a href="javascript:void(0);">删除</a></td>
    </tr>

    <tr>
        <td><input name="cb" type="checkbox"></td>
        <td>3</td>
        <td>岳不群</td>
        <td>?</td>
        <td><a href="javascript:void(0);">删除</a></td>
    </tr>

</table>
<div>
    <input id="selectAll" type="button" value="全选">
    <input id="unSelectAll" type="button" value="全不选">
    <input id="selectRev" type="button" value="反选">
</div>
</body>
</html>