<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
table {
text-align: center;
width: 350px;
height: 200px;
margin: 100px auto;
background: rgb(245, 220, 224);
}
thead {
background: rgb(36, 63, 235);
}
td {
border: 1px solid #ccc;
}
</style>
<body>
<table>
<thead>
<th><input type="checkbox" class="all"></th>
<th>商品</th>
<th>价钱</th>
</thead>
<tr>
<td><input type="checkbox" class="one"></td>
<td>苹果</td>
<td>$4</td>
</tr>
<tr>
<td><input type="checkbox" class="one"></td>
<td>香蕉</td>
<td>$8</td>
</tr>
<tr>
<td><input type="checkbox" class="one"></td>
<td>西瓜</td>
<td>$14</td>
</tr>
<tr>
<td><input type="checkbox" class="one"></td>
<td>猕猴桃</td>
<td>$9</td>
</tr>
</table>
<script>
var all = document.querySelector('.all');
var one = document.querySelectorAll('.one');
all.onclick = function () {
if (this.checked === true) {
for (var i = 0; i < one.length; i++) {
one[i].checked = true;
}
} else {
for (var i = 0; i < one.length; i++) {
one[i].checked = false;
}
}
}
</script>
</body>
</html>