html代码:
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 7 <style></style> 8 9 <script> 10 window.onload = function () { 11 var p1 = document.getElementById('id1'); 12 var p2 = document.getElementById('id2'); 13 var p3 = document.getElementById('id3'); 14 15 var q = document.getElementById('div1'); 16 var w = q.getElementsByTagName('input'); 17 18 p1.onclick = function () { 19 for (var i = 0; i < w.length; i++) 20 w[i].checked = true;//复选框会变成选中状态 21 }; 22 23 p2.onclick = function () { 24 for (var i = 0; i < w.length; i++) 25 w[i].checked = false; 26 } 27 28 p3.onclick = function () { 29 for (var i = 0; i < w.length; i++) { 30 if (w[i].checked == true) 31 w[i].checked = false; 32 else 33 w[i].checked = true; 34 } 35 } 36 37 }; 38 </script> 39 40 </head> 41 42 <body> 43 <input id="id1" type="button" value="全选"/><br> 44 <input id="id2" type="button" value="不选"/><br> 45 <input id="id3" type="button" value="反选"/><br> 46 <div id="div1"> 47 <input type="checkbox"/><br> 48 <input type="checkbox"/><br> 49 <input type="checkbox"/><br> 50 <input type="checkbox"/><br> 51 <input type="checkbox"/><br> 52 <input type="checkbox"/><br> 53 </div> 54 </body> 55 56 </html>
浏览器中效果: