未进化的程序猿
人生最苦痛的是梦醒了无路可走。做梦的人是幸福的;倘没有看出可走的路,最要紧的是不要去惊醒他。鲁迅
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <form action = "#" method = "">
            <input id="all" type="checkbox"/>全选
            <hr />
            <div>
                <input type="checkbox" class="box"/>篮球
                <br/> 
                <input type="checkbox" class="box"/>排球
                <br/>
                <input type="checkbox" class="box"/>气球
                <br/>
                <input type="checkbox" class="box"/>足球
                <br/>
            </div>
        </form>
    </body>
</html>

<script>
        window.onload = function (){
        var all = document.getElementById ("all");            //全选checkbox
        var box = document.getElementsByClassName('box');    //子复选框
        
        //遍历checkbox,把子复选框的checked设置成全选框的状态,实现全选/全不选
        all.onclick = function (){
            for ( var i = 0; i < box.length; i++){
                box[i].checked = this.checked;
            }
        };
        //遍历checkbox,子复选框有一个未选中时,如果全选框设为false不选状态
        for ( var i = 0; i < box.length; i++){
            box[i].onclick = function (){
                if ( !this.checked ){
                    all.checked = false;
                }else{
                    all.checked = true;
                }
            };
        }
    }
    </script>

posted on 2020-11-27 15:59  甘茂旺  阅读(277)  评论(0编辑  收藏  举报