- 全选和取消全选做法:让下面所有复选框的 checked 属性(选中状态)跟随全选按钮即可;
- 下面复选框需要全部选中,上面全选才能选中做法:给下面所以复选框绑定点击事件,每次点击,都要循环查看下面所有的复选框是否有没选中的,上面全选就不选中;
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <title></title> </head> <body> <input type="checkbox" name="" id="" class="check">全选/取消全选 <br> <input type="checkbox" name="nan" id="" class="ball_checkbox">足球<br> <input type="checkbox" name="nan" id="" class="ball_checkbox">棒球<br> <input type="checkbox" name="nan" id="" class="ball_checkbox">篮球<br> <input type="checkbox" name="nan" id="" class="ball_checkbox">橄榄球<br> <script type="text/javascript"> let delete_checkbox = document.getElementsByClassName('check')[0]; let arr = true; delete_checkbox.addEventListener('click', function() { let nan = document.getElementsByName('nan'); for (let i = 0; i < nan.length; i++) { nan[i].checked = this.checked; } }) </script> </body> </html>
09-01
222
05-21
394
10-23
323
05-06
6966