摘要:<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>全選</title> <link rel="stylesheet" type="text/css"
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>全選</title> <link rel="stylesheet" type="text/css" href="../css/css.css"> <link rel="shortcut icon" type="image/x-icom" href="../picture/mi.png"> <style type="text/css"> .box{width: 100px;height: 200px;border: 1px solid #000;border-radius: 5px;margin: 20px auto;padding:8px 10px;} .box div{border-bottom: 1px solid #000;margin-bottom: 15px;} .box input{margin: 8px;} </style> </head> <body> <script type="text/javascript"> function checkall (){ var checkall,item; checkall=document.getElementById("checkall");//獲取全選 item=document.getElementsByName("item[]");//獲取下面的勾選框 for (i =0;i<item.length;i++){ if (checkall.checked){ item[i].checked=true; }else{ item[i].checked=false; } } } </script> <div class="box"> <div><input type="checkbox" id="checkall" onclick="checkall()"><label for="chechall">全選</label><br> </div> <input type="checkbox" name="item[]">選項1<br> <input type="checkbox" name="item[]">選項2<br> <input type="checkbox" name="item[]">選項3<br> <input type="checkbox" name="item[]">選項4<br> <input type="checkbox" name="item[]">選項5<br> </div> </body> </html>
老師這一節(jié)課我感覺好多都沒有懂,特別是哪一個for循環(huán)item.length求取字符串的長度并且返回,但是勾選框有什么字符串呀?
還有就是全選框的地方為什么要加一個<label></label for ="id名">,我有試過不加這個label標(biāo)簽,全選框的功能也是可以正常實現(xiàn)的。
這節(jié)課就是這幾個地方感覺好模糊,沒有什么概念了。
批改老師:滅絕師太批改時間:2018-11-28 09:18:42
老師總結(jié):<label> 標(biāo)簽為 input 元素定義標(biāo)注,這是一種寫法習(xí)慣 for 屬性應(yīng)當(dāng)與相關(guān)元素的 id 屬性相同其實就是添加了一個關(guān)聯(lián);這些可以百度清楚啊!
item.length 獲取的是下面的勾選框長度