闲扯 Javascript 02 全选、不选、反选

 1 <body>
 2 <input id="btn1" type="button" value="全选" /><br>
 3 <input id="btn2" type="button" value="不选" /><br>
 4 <input id="btn3" type="button" value="反选" /><br>
 5 <div id="div1">
 6     <input type="checkbox" /><br>
 7     <input type="checkbox" /><br>
 8     <input type="checkbox" /><br>
 9     <input type="checkbox" /><br>
10     <input type="checkbox" /><br>
11     <input type="checkbox" /><br>
12     <input type="checkbox" /><br>
13     <input type="checkbox" /><br>
14     <input type="checkbox" /><br>
15     <input type="checkbox" /><br>
16     <input type="checkbox" /><br>
17     <input type="checkbox" /><br>
18     <input type="checkbox" /><br>
19     <input type="checkbox" /><br>
20     <input type="checkbox" /><br>
21     <input type="checkbox" /><br>
22     <input type="checkbox" /><br>
23 </div>
24 </body>
 1 <script>
 2  window.onload=function ()
 3  {
 4      
 5      var oBtn1=document.getElementById('btn1');
 6      var oBtn2=document.getElementById('btn2');
 7      var oBtn3=document.getElementById('btn3');
 8      var oDiv=document.getElementById('div1');
 9      var aCh=oDiv.getElementsByTagName('input');
10      
11      oBtn1.onclick=function ()
12      {
13           for(var i=0;i<aCh.length;i++)
14           {
15               aCh[i].checked=true;
16               }  
17             
18      }
19      oBtn2.onclick=function ()
20      {
21           for(var i=0;i<aCh.length;i++)
22           {
23               aCh[i].checked=false;
24               }  
25             
26      }
27       oBtn3.onclick=function ()
28      {
29            for(var i=0;i<aCh.length;i++)
30           {
31               if(aCh[i].checked==true)
32               {  
33                    aCh[i].checked=false;  
34                   }
35                   else
36                   {
37                         aCh[i].checked=true;
38                       }
39               
40              
41              
42               } 
43             
44      }
45 
46      
47 }
48 
49 
50 </script>
原文地址:https://www.cnblogs.com/izhiniao/p/3678698.html