js实现全选

Original 2019-03-19 16:23:36 215
abstract:js 实现全选小功能 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="wi
js 实现全选小功能
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box{
width: 100px;
height: 260px;
border: 1px solid #ccc;
border-radius: 5px;
padding: 5px 10px;
margin: 20px auto;
}

.box div{
border-bottom: 1px solid #ccc;
padding-bottom: 10px;
}

.box input{
margin: 10px;
}
</style>
</head>

<body>
<div class="box">
<div>
<input type="checkbox" id="checkall" onclick="checkall()"><label for="checkall">全选</label>
</div>
<input type="checkbox" >选项1<br>
<input type="checkbox" >选项2<br>
<input type="checkbox" >选项3<br>
<input type="checkbox" >选项4<br>
<input type="checkbox" >选项5<br>
<input type="checkbox" >选项6<br>
</div>

<script>
function checkall() {
var checkall = document.getElementById('checkall');
var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
if (checkall.checked) {
inputs[i].checked = true;
} else {
inputs[i].checked = false;
}
}
}
</script>
</body>

</html>


Correcting teacher:天蓬老师Correction time:2019-03-19 16:48:55
Teacher's summary:你选上以后怎么处理呢, 用什么名称来引用?name属性不能省

Release Notes

Popular Entries