摘要:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>全选</title> <style> .box{  
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>全选</title>
<style>
.box{
width:120px;
height:250px;
border:1px solid #000;
border-radius:10px;
padding:10px;
margin:50px auto;
background:pink;
}
.box div{
border-bottom:1px solid #000;
padding-bottom:6px;
}
input{
margin:11px 15px;
}
</style>
</head>
<body>
<div>
<div class="box">
<input type="checkbox" id="checkall" onclick="checkAll()"><label for="checkall">全选</label>
</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>
<input type="checkbox" name="item[]">选项6<br>
</div>
<script>
function checkAll(){
var checkall,item;
checkall=document.getElementById('checkall');
item=document.getElementsByName('item[]');
for(var i=0;i<item.length;i++){
if(checkall.checked){
item[i].checked=true;
}else{
item[i].checked=false;
}
}
}
</script>
</body>
</html>总结:本节课讲述的是JS的经典案列:全选,在学习的过程中要认真仔细,查找的过程中,要认清是id、class或name,除了id外,其它的都要加s,在循环的时候,条件要判断清楚,逻辑要清晰,多多练习,就可以完成了。
批改老师:西门大官人批改时间:2019-04-02 09:24:27
老师总结:如果学习过jquery,还可以用jquery实现同样的效果,不需要循环