abstrakt:<!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; //當全選框被選中時,下面的勾選框選中 }else{ item[i].checked=false; //反之取消選中 } } } </script> </head> <body> <div> <input type="checkbox" name="item[]"><span>選項1</span><br> <input type="checkbox" name="item[]"><span>選項2</span><br> <input type="checkbox" name="item[]"><span>選項3</span><br> <input type="checkbox" name="item[]"><span>選項4</span><br> <input type="checkbox" name="item[]"><span>選項5</span><br> <input type="checkbox" name="item[]"><span>選項6</span><br> <input type="checkbox" name="item[]"><span>選項7</span><br> <input type="checkbox" name="item[]"><span>選項8</span><br> <div> <input type="checkbox" id="checkall" onclick="checkall()" name=""><label for="checkall">全選</label> </div> </div> </body> </html>
總結:
for (var i=0 ; i<item.length; i++){}
本來for里面第2個參數(shù),結束長度里為什么放length有點懵逼,然后console.log(item)后發(fā)現(xiàn)取出的數(shù)據(jù)里,有個length長度(這里應該是個數(shù))有幾個item。然后item[] 后面的[]是數(shù)組下標預留的放括號?
Korrigierender Lehrer:滅絕師太Korrekturzeit:2018-12-17 09:27:52
Zusammenfassung des Lehrers:表示變量item獲取的是一個數(shù)組對象?。∑鋵嵅焕斫獾臏y試測試就明白了,繼續(xù)加油!