美文网首页
js实现模态对话框 全选 反选

js实现模态对话框 全选 反选

作者: xin激流勇进 | 来源:发表于2017-12-08 20:51 被阅读0次
image.png
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .hide{
            display: none;
        }

        .c1{
            position: fixed;
            left: 0px;
            right: 0px;
            top: 0px;
            bottom: 0px;
            background-color: black;
            opacity: 0.6;
            z-index: 5;
        }

        .c2{
            position: fixed;
            left: 50%;
            top: 50%;
            height: 400px;
            width: 500px;
            margin-top: -200px;
            margin-left: -250px;
            background-color: white;
            z-index: 10;
        }
        .c2 div{
            position: relative;
            left: 50%;
            top:50%;
            width: 200px;
            height: 100px;
            /*background-color: #60add5;*/
            margin-left: -100px;
            margin-top: -50px;

        }
    </style>
</head>
<body style="margin: 0;">
    <div>
        <input type="button" value="添加" onclick="ShowModel();">
        <input type="button" value="全选" onclick="ChooseAll();">
        <input type="button" value="取消" onclick="CancelAll();">
        <input type="button" value="反选" onclick="ReverseAll();">

        <table>
            <thead >
                <tr>
                    <th>选择</th>
                    <th>主机名</th>
                    <th>端口</th>
                </tr>
            </thead>
            <tbody id="tb">
                <tr>
                    <td>
                        <input type="checkbox">
                    </td>
                    <td>1.1.1.1</td>
                    <td>80</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox">
                    </td>
                    <td>1.1.2.1</td>
                    <td>180</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox">
                    </td>
                    <td>1.1.3.1</td>
                    <td>810</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox">
                    </td>
                    <td>1.1.4.1</td>
                    <td>801</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox">
                    </td>
                    <td>1.1.5.1</td>
                    <td>800</td>
                </tr>
            </tbody>
        </table>
    </div>
    <!--遮罩层开始-->
    <div id="i1" class="c1 hide"></div>
    <!--遮罩层结束-->

    <!--弹出框-->
    <div id="i2" class="c2 hide">
        <div>
            <p><input type="text"></p>
            <p><input type="text"></p>
            <p><input type="button" value="取消" onclick="HideModel();"><input type="button" value="确定"></p>
        </div>
    </div>
    
    <script>
        function ShowModel() {
            document.getElementById('i1').classList.remove('hide');
            document.getElementById('i2').classList.remove('hide');
        }

        function HideModel() {
            document.getElementById('i1').classList.add('hide');
            document.getElementById('i2').classList.add('hide');
        }
        
        function ChooseAll() {
            var tbody = document.getElementById('tb');
            var tr_list = tbody.children;
            for(var i=0;i<tr_list.length;i++){
                var current_tr = tr_list[i];
                var check_box = current_tr.children[0].children[0];
                check_box.checked = true;
            }
        }

        function CancelAll() {
            var tbody = document.getElementById('tb');
            var tr_list = tbody.children;
            for(var i=0;i<tr_list.length;i++){
                var current_tr = tr_list[i];
                var check_box = current_tr.children[0].children[0];
                check_box.checked = false;
            }
        }

        function ReverseAll() {
            var tbody = document.getElementById('tb');
            var tr_list = tbody.children;
            for(var i=0;i<tr_list.length;i++){
                var current_tr = tr_list[i];
                var check_box = current_tr.children[0].children[0];
                if(check_box.checked){
                    check_box.checked = false;
                }else{
                    check_box.checked = true;

                }
            }
        }
    </script>
</body>
</html>

相关文章

网友评论

      本文标题:js实现模态对话框 全选 反选

      本文链接:https://www.haomeiwen.com/subject/qfeyixtx.html