首页 > 代码库 > js-03课-02 复选框全选 , 全不选, 反选

js-03课-02 复选框全选 , 全不选, 反选

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<input type="button" value="http://www.mamicode.com/反选">
<input type="button" value="http://www.mamicode.com/全选">
<input type="button" value="http://www.mamicode.com/全不选">
<ul>
    <li><input type="checkbox" checked></li>
    <li><input type="checkbox" ></li>
    <li><input type="checkbox" ></li>
    <li><input type="checkbox" ></li>
    <li><input type="checkbox" checked></li>
</ul>

<script>
    window.onload = function(){
        var aInput = document.getElementsByTagName(‘input‘);
        aInput[0].onclick = function(){
            for(var i = 3; i < aInput.length; i++){
                var isChecked = aInput[i].checked
                aInput[i].checked = !isChecked;
            }
        }
        aInput[1].onclick = function(){
            for(var i = 3; i < aInput.length; i++){
                aInput[i].checked = true;
            }
        }
        aInput[2].onclick = function(){
                for(var i = 3; i < aInput.length; i++){
                    aInput[i].checked = false;
                }
        }
    }

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

  

js-03课-02 复选框全选 , 全不选, 反选