<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
table td {
border: 1px solid #666;
text-align: center;
width: 204px;
}
table {
border-collapse: collapse;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<td><input class="all" type="checkbox"></td>
<td>书名</td>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox"></td>
<td>下一站拉萨</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>明朝那些事</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>青年文摘</td>
</tr>
</tbody>
</table>
<script>
var checkall = document.querySelector(".all");
var check = document.querySelector("tbody").querySelectorAll("input");
checkall.onclick = function () {
for (var i = 0; i < check.length; i++) {
check[i].checked = this.checked;
}
}
for (var i = 0; i < check.length; i++) {
check[i].onclick = function () {
var flag = true;
for (var i = 0; i < check.length; i++) {
if (!check[i].checked) {
flag = false;
break;
}
}
checkall.checked = flag;
}
}
</script>
</body>
</html>
点击·最上面checkbox实现全选,下面又一个不选中,则全选按钮不打勾