<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> *{margin: 0; padding: 0; list-style: none;} ul{margin: 150px 250px} </style> </head> <body> <ul> <li><input type="checkbox" id="all">全选</li> <li><input type="checkbox">1</li> <li><input type="checkbox">2</li> <li><input type="checkbox">3</li> <li><input type="checkbox">4</li> </ul> <script> window.onload=function(){ var all=document.getElementById("all"); var one=document.getElementsByTagName("input"); all.onclick=function(){ for(var i=1;i<one.length;i++){ one[i].checked=all.checked; } } for(var i=1;i<one.length;i++){ one[i].onclick=function(){ var num=0; for(var j=1;j<one.length;j++) { if(one[j].checked==true) { num++; } } if(num==one.length-1) { all.checked=true; } else{ all.checked=false; } } } } </script> </body> </html>
js实现全选效果
最新推荐文章于 2023-12-12 10:58:38 发布
