js实现checkbox全选以及反选

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        select{
            margin-left: 100px;
        }
    </style>
</head>
<body>
<input type="checkbox" name="fruit" value="1"><br/>
<input type="checkbox" name="fruit" value="1"><br/>
<input type="checkbox" name="fruit" value="1"><br/>
<input type="checkbox" name="fruit" value="1"><br/>
<input type="checkbox" name="fruit" value="1"><br/>
<input type="checkbox" name="fruit" value="1"><br/>
<input type="checkbox" name="fruit" value="1"><br/>
<input type="checkbox" name="fruit" value="1"><br/>
<input type="checkbox" name="fruit" value="1"><br/>
<button type="button" onclick="selectAll()">全选</button>
<button type="button" onclick="unSelectAll()">全不选</button>
<button type="button" onclick="converSelectAll()">反选</button>
<script type="text/javascript">
    var inputs =  document.getElementsByTagName("input");
    function selectAll(){
        for(var i = 0;i<inputs.length;i++){
            inputs[i].checked = true;
        }
    }
    function unSelectAll(){
        for(var i = 0;i<inputs.length;i++){
            inputs[i].checked = false;
        }
    }
    function converSelectAll(){
        for(var i = 0;i<inputs.length;i++){
            if(inputs[i].checked){
                inputs[i].checked = false;
            }else{
                inputs[i].checked = true;
            }
        }
    }

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

 

上一篇:将自己本地开发的项目映射到外网。可以用域名访问,将内网IP映射成对外可访问的域名


下一篇:电商4.0项目【三】: 用户模块(8081)