全选按钮的HTML代码
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html" charset="UTF-8"> <title>Title</title> </head> <body> <div id="btn"> <input type="checkbox" />全选<br /> </div> <div> <input type="checkbox" /><br /> <input type="checkbox" /><br /> <input type="checkbox" /><br /> <input type="checkbox" /><br /> </div> </body> </html>
js代码
window.onload = function () { var oBtn = document.getElementById('btn'); var aInput = document.getElementsByTagName('input'); oBtn.onclick=function () { //var i; if(aInput[0].checked===true) { for(var i=1; i<aInput.length; i++) aInput[i].checked=true; } else{ for(var i=1; i<aInput.length; i++) aInput[i].checked=false; } for(var i=1; i<aInput.length; i++) { aInput[i].onclick=function () { if(!this.checked) { aInput[0].checked=false; } } } } }
慢慢来总会有收获