摘要:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> .boxm{ width:120px; he
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.boxm{
width:120px;
height:300px;
/*background:tomato;*/
margin:0 auto;
border:1px solid black;
border-radius:5px;
padding:5px;
box-sizing:border-box;
}
.boxm div{width:100px;height:40px;border-bottom:1px solid black;margin:20px auto;}
.boxm input{margin:10px;}
</style>
</head>
<body>
<div>
<div>
<input type="checkbox" id="checkall" onclick="checkAll()"><label for="checkall">全选</label>
</div>
<input type="checkbox" name="item[]">选项一
<input type="checkbox" name="item[]">选项二
<input type="checkbox" name="item[]">选项三
<input type="checkbox" name="item[]">选项四
<input type="checkbox" name="item[]">选项五
<input type="checkbox" name="item[]">选项六
</div>
<script type="text/javascript">
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>有了思路就能一步一步敲出来,但是代码运用还不够熟练,虽然想到大概要怎么做,可是上手却不知道应该先干嘛后干嘛了。
首先获取到需要用到的属性,然后输入事件,再然后返回结果
批改老师:天蓬老师批改时间:2018-11-06 19:16:32
老师总结:边写边测试,是一个好习惯.
其实这样的全选操作,有很多现成的构件可以实现,但是了解原生底层是如何实现的,也是非常重要,且有必要的