????:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>T全選</title> <style type="text/css"> .box{width: 150px;heig
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>T全選</title> <style type="text/css"> .box{width: 150px;height: 300px;border: 3px solid #ff6700;border-radius: 5px,padding:5px 10px;margin: 20px auto;} .box div{border-top: 1px solid #ff6700;padding-top: 10px;margin-bottom: 8px;} .box input{margin:9px;font-size: 10px;} span{color:#ff6700;} </style> <script type="text/javascript"> function checkall() { var checkall,tiem; checkall=document.getElementById('checkall') //獲取全選 item=document.getElementsByName("item[]") //獲取下面的勾選框 console.log(item) for (var i=0 ; i<item.length; i++) { if(checkall.checked){ item[i].checked=true; //當(dāng)全選框被選中時(shí),下面的勾選框選中 }else{ item[i].checked=false; //反之取消選中 } } } </script> </head> <body> <div> <input type="checkbox" name="item[]"><span>選項(xiàng)1</span><br> <input type="checkbox" name="item[]"><span>選項(xiàng)2</span><br> <input type="checkbox" name="item[]"><span>選項(xiàng)3</span><br> <input type="checkbox" name="item[]"><span>選項(xiàng)4</span><br> <input type="checkbox" name="item[]"><span>選項(xiàng)5</span><br> <input type="checkbox" name="item[]"><span>選項(xiàng)6</span><br> <input type="checkbox" name="item[]"><span>選項(xiàng)7</span><br> <input type="checkbox" name="item[]"><span>選項(xiàng)8</span><br> <div> <input type="checkbox" id="checkall" onclick="checkall()" name=""><label for="checkall">全選</label> </div> </div> </body> </html>
總結(jié):
for (var i=0 ; i<item.length; i++){}
本來(lái)for里面第2個(gè)參數(shù),結(jié)束長(zhǎng)度里為什么放length有點(diǎn)懵逼,然后console.log(item)后發(fā)現(xiàn)取出的數(shù)據(jù)里,有個(gè)length長(zhǎng)度(這里應(yīng)該是個(gè)數(shù))有幾個(gè)item。然后item[] 后面的[]是數(shù)組下標(biāo)預(yù)留的放括號(hào)?
?? ???:滅絕師太?? ??:2018-12-17 09:27:52
???? ??:表示變量item獲取的是一個(gè)數(shù)組對(duì)象??!其實(shí)不理解的測(cè)試測(cè)試就明白了,繼續(xù)加油!