JS-事件-关于全选

代码

<!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: aqua;
        }
    </style>
    <script>
        //页面加载完后绑定事件
        window.onload = function () {
            //单击全选
            document.getElementById("selectAll").onclick = function () {
                let cs = document.getElementsByName("c");
                for (let i = 0; i < cs.length; i++) {
                    //设置状态为选中
                    cs[i].checked = true;
                }
            }
            //全不选
            document.getElementById("unSelectAll").onclick =  function () {
                let cs = document.getElementsByName("c");
                for (let i = 0; i < cs.length; i++) {
                    cs[i].checked = false;
                }
            }

            //反选
            document.getElementById("selectRev").onclick = function () {
                let cs = document.getElementsByName("c");
                for (let i = 0; i < cs.length; i++) {
                    cs[i].checked = !cs[i].checked;
                }
            }

            document.getElementById("firstC").onclick = function (){
                let cs = document.getElementsByName("c");
                let first = document.getElementById("firstC");
                for (let i = 1; i < cs.length; i++) {
                    cs[i].checked = first.checked;
                }
            }
            //给所有的tr绑定鼠标移到元素之上和移出元素事件
            let trs = document.getElementsByTagName("tr");
            //遍历
            for (let i = 0; i < trs.length; i++) {
                //移到元素之上(鼠标移动到这时)
                trs[i].onmouseover = function () {
                    this.className = "over";
                }
                //鼠标移开后
                trs[i].onmouseout = function () {
                    this.className = "out";
                }
            }

        }
    </script>
    

</head>
<body>

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

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

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

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

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

解析:

JS-事件-关于全选
JS-事件-关于全选
JS-事件-关于全选

上一篇:Java之Integer$IntegerCache初探(AutoBoxCacheMax)


下一篇:Java实现cs架构