js的全选小案例
枫2019-05-03 22:38:54232<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>全选</title>
<style>
.box{width: 120px;height: 220px;border: 1px solid #000;margin: 10px auto;padding: 5px 10px}
.box div{border-bottom: 1px solid #444444;padding-bottom: 8px;margin-bottom: 8px}
input{margin: 6px 10px}
</style>
<script>
function checkAll() {
var checkall=document.getElementById('checkall') //变量获取id
var one=document.getElementsByClassName('item') //变量获取class
for(var i=0;i<one.length;i++ ){ //循环语句
if(checkall.checked){ //判断checkall是否被勾选
one[i].checked=true //item全选
} else{
one[i].checked=false //否则不
}
}
}
</script>
</head>
<body>
<div class="box">
<div>
<input type="checkbox" id="checkall" onclick="checkAll()"><label for="checkall">全选</label>
</div>
<input type="checkbox" class="item">选项1<br>
<input type="checkbox" class="item">选项2<br>
<input type="checkbox" class="item">选项3<br>
<input type="checkbox" class="item">选项4<br>
<input type="checkbox" class="item">选项5<br>
<input type="checkbox" class="item">选项6<br>
</div>
</body>
</html>