列表

详情


JS29. 全选

描述

请补全JavaScript代码,实现以下效果:
1. 选中"全选"框,以下所有选项全部勾选。
2. 把"全选"框从选中状态勾选成未选中状态,其他复选框全部取消选中效果。
3. 当其他复选框全部选中,"全选框"为选中状态。
4. 当其他复选框有一个未选中,"全选框"取消选中状态。
注意:
1. 必须使用DOM0级标准事件(onchange)

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 1695ms, 内存消耗: 77876KB, 提交时间: 2022-02-10

{"css":"","js":"","html":"<!DOCTYPE html>\n<html lang=\"en\">\n    <head>\n        <meta charset=\"UTF-8\">\n    </head>\n        <style>\n            ul {\n                list-style: none;\n            }\n        </style>\n    <body>\n        <ul>\n            <li>全选<input type='checkbox' id='all'></li>\n            <li>Java<input type='checkbox' class='item'></li>\n            <li>javaScript<input type='checkbox' class='item'></li>\n            <li>C++<input type='checkbox' class='item'></li>\n            <li>python<input type='checkbox' class='item'></li>\n            <li>.net<input type='checkbox' class='item'></li>\n        </ul>\n\n        <script>\n            // 补全代码\n            var all = document.querySelector('#all')\nvar options = Array.from(document.querySelectorAll('.item'))\nall.onchange = () => {\n    options.forEach(x => x.checked = all.checked)\n}\noptions.forEach((item)=>{\n    item.onchange = function() {\n        if(!this.checked){\n            all.checked = false\n        }else{\n            if(options.every(x => x.checked)){\n                all.checked = true\n            }else{\n                all.checked = false\n            }\n        }\n    }\n})\n\n            \n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1768ms, 内存消耗: 77916KB, 提交时间: 2021-12-22

{"css":"","js":"","html":"<!DOCTYPE html>\n<html lang=\"en\">\n    <head>\n        <meta charset=\"UTF-8\">\n    </head>\n        <style>\n            ul {\n                list-style: none;\n            }\n        </style>\n    <body>\n        <ul>\n            <li>全选<input type='checkbox' id='all'></li>\n            <li>Java<input type='checkbox' class='item'></li>\n            <li>javaScript<input type='checkbox' class='item'></li>\n            <li>C++<input type='checkbox' class='item'></li>\n            <li>python<input type='checkbox' class='item'></li>\n            <li>.net<input type='checkbox' class='item'></li>\n        </ul>\n\n          <script>\n        var all = document.getElementById(\"all\")\n        var options = Array.from(document.querySelectorAll(\".item\"))\n        // 补全代码\n        all.onchange = () => {\n            options.forEach((item) => {\n                item.checked = all.checked\n            })\n        }\n        options.forEach((item) => {\n            item.onchange = function () {\n                let isCheckAll = true\n                options.forEach((item) => {\n                    if (!item.checked) {\n                        isCheckAll = false\n                    }\n                })\n                all.checked = isCheckAll\n            }\n        })\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1769ms, 内存消耗: 77900KB, 提交时间: 2021-12-06

{"css":"","js":"","html":"<!DOCTYPE html>\n<html lang=\"en\">\n    <head>\n        <meta charset=\"UTF-8\">\n    </head>\n        <style>\n            ul {\n                list-style: none;\n            }\n        </style>\n    <body>\n        <ul>\n            <li>全选<input type='checkbox' id='all'></li>\n            <li>Java<input type='checkbox' class='item'></li>\n            <li>javaScript<input type='checkbox' class='item'></li>\n            <li>C++<input type='checkbox' class='item'></li>\n            <li>python<input type='checkbox' class='item'></li>\n            <li>.net<input type='checkbox' class='item'></li>\n        </ul>\n\n        <script>\n            // 补全代码\n            var all = document.querySelector('#all');\n            var options = Array.from(document.querySelectorAll('.item'));\n            all.onchange = () =>{\n                options.forEach(x => x.checked = all.checked);\n            }\n            options.forEach((item) => {\n                item.onchange = function(){\n                    if (! this.checked){\n                        all.checked = false;\n                    }else {\n                        if (options.every(x => x.checked)){\n                            all.checked = true;\n                        }else{\n                            all.checked = false;\n                        }\n                    }\n                }\n            })\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1774ms, 内存消耗: 77832KB, 提交时间: 2021-12-18

{"css":"","js":"","html":"<!DOCTYPE html>\n<html lang=\"en\">\n    <head>\n        <meta charset=\"UTF-8\">\n    </head>\n        <style>\n            ul {\n                list-style: none;\n            }\n        </style>\n    <body>\n        <ul>\n            <li>全选<input type='checkbox' id='all'></li>\n            <li>Java<input type='checkbox' class='item'></li>\n            <li>javaScript<input type='checkbox' class='item'></li>\n            <li>C++<input type='checkbox' class='item'></li>\n            <li>python<input type='checkbox' class='item'></li>\n            <li>.net<input type='checkbox' class='item'></li>\n        </ul>\n\n        <script>\n            // 补全代码\n                var all = document.querySelector('#all')\n                var options = Array.from(document.querySelectorAll('.item'))\n                all.onchange = () => {\n                    options.forEach(x => x.checked = all.checked)\n                }\n               options.forEach((item)=>{\n    item.onchange = function() {   \n//减去了多余的一个if判断\n            if(options.every(x => x.checked)){\n                all.checked = true\n            }else{\n                all.checked = false\n            }\n    }\n})\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1777ms, 内存消耗: 77836KB, 提交时间: 2021-12-21

{"css":"","js":"","html":"<!DOCTYPE html>\n<html lang=\"en\">\n    <head>\n        <meta charset=\"UTF-8\">\n    </head>\n        <style>\n            ul {\n                list-style: none;\n            }\n        </style>\n    <body>\n        <ul>\n            <li>全选<input type='checkbox' id='all'></li>\n            <li>Java<input type='checkbox' class='item'></li>\n            <li>javaScript<input type='checkbox' class='item'></li>\n            <li>C++<input type='checkbox' class='item'></li>\n            <li>python<input type='checkbox' class='item'></li>\n            <li>.net<input type='checkbox' class='item'></li>\n        </ul>\n\n        <script>\n            // 补全代码\n             var all = document.getElementById(\"all\")\n        var options = Array.from(document.querySelectorAll(\".item\"))\n        // 补全代码\n        all.onchange = () => {\n            options.forEach((item) => {\n                item.checked = all.checked\n            })\n        }\n        options.forEach((item) => {\n            item.onchange = function () {\n                let isCheckAll = true\n                options.forEach((item) => {\n                    if (!item.checked) {\n                        isCheckAll = false\n                    }\n                })\n                all.checked = isCheckAll\n            }\n        })\n        </script>\n    </body>\n</html>","libs":[]}

上一题