JS 实现全选

链接

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
    </head>
        <style>
            ul {
                list-style: none;
            }
        </style>
    <body>
        <ul>
            <li>全选<input type='checkbox' id='all'></li>
            <li>Java<input type='checkbox' class='item'></li>
            <li>javaScript<input type='checkbox' class='item'></li>
            <li>C++<input type='checkbox' class='item'></li>
            <li>python<input type='checkbox' class='item'></li>
            <li>.net<input type='checkbox' class='item'></li>
        </ul>

        <script>
            // 补全代码
            var all = document.querySelector("#all");
            var options = Array.from(document.querySelectorAll(".item"));
            all.onchange = function() {
                for(let i of options) {
                    i.checked = all.checked;
                }
            }
            function updateAllbtn() {
                let r = true;
                for(let i of options) {
                    r = r&&i.checked;
                }
                if(r) {
                    all.checked = true;
                } else {
                    all.checked = false;
                }
            }
            options.forEach(i => {
                i.onchange = function() {
                    updateAllbtn();
                }
            })
            
        </script>
    </body>
</html>
全部评论

相关推荐

不愿透露姓名的神秘牛友
10-04 05:12
kalistar:简历留六个字,北京大学(本科),黑体加粗,看看哪个hr不长眼敢碰瓷我们北大✌
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务