前端學習(1624):前端系列實戰課程之全選和反選

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="button" value="全選" id="all">
    <input type="button" value="不選" id="no">
    <input type="button" value="反選" id="rev">
    <div id="box">
        <input type="checkbox" name="" id="">
        <input type="checkbox" name="" id="">
        <input type="checkbox" name="" id="">
    </div>
    <script>
        window.onload = function() {
            var oAll = document.getElementById('all');
            var oNo = document.getElementById('no');
            var oRev = document.getElementById('rev');
            var oBox = document.getElementById('box');
            var aInp = oBox.getElementsByTagName('input');
            oAll.onclick = function() {
                for (var i = 0; i < aInp.length; i++) {
                    aInp[i].checked = true;
                }
            };
            oNo.onclick = function() {
                for (var i = 0; i < aInp.length; i++) {
                    aInp[i].checked = false;
                }
            };
            oRev.onclick = function() {
                for (var i = 0; i < aInp.length; i++) {
                    aInp[i].checked = !aInp[i].checked;
                }
            }

        }
    </script>
</body>

</html>

運行結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章