【案例】列表全选、全不选、反选

注意点:

checkbox.checked = true;

checkbox.checked = false;

<!DOCTYPE html>

<html lang="en">

<head>

        <meta charset="UTF-8">

        <title>列表全选、全不选、反选</title>

</head>

<body>

        <h1>最爱的水果列表</h1>

        <hr>

        <input type="checkbox" id="apple"><label for="apple">苹果</label><br><br>

        <input type="checkbox" id="str"><label for="str">草莓</label><br><br>

        <input type="checkbox" id="ban"><label for="ban">香蕉</label><br><br>

        <input type="checkbox" id="liu"><label for="liu">榴莲</label><br><br>

        <input type="checkbox" id="san"><label for="san">山竹</label><br><br>

        <input type="checkbox" id="che"><label for="che">车厘子</label><br><br>

        <button id="all">全选</button>

        <button id="allNo">全不选</button>

        <button id="reverse">反选</button>

</body>

<script>

        var checkboxs = document.querySelectorAll('input[type=checkbox]');

        var all = document.getElementById('all');

        var allNo = document.getElementById('allNo');

        var reverse = document.getElementById('reverse');

        all.onclick = function(){

                 for(var i in checkboxs){

                         checkboxs[i].checked = true;

                 }

        }

        allNo.onclick = function(){

                 for(var i in checkboxs){

                         checkboxs[i].checked = false;

                 }

        }

        reverse.onclick = function(){

                 for(var i in checkboxs){

                         checkboxs[i].checked = ! checkboxs[i].checked;

                 }

        }

</script>

</html>

猜你喜欢

转载自www.cnblogs.com/sherryStudy/p/all_option.html