<html>
<head>
<meta charset="utf-8">
<title>全选2</title>
<style type="text/css">
.wrap {
margin-left: 500px;
margin-top: 200px;
}
table {
border-collapse: collapse;
}
th {
border: 1px solid black;
height: 45px;
}
td {
border: 1px solid black;
text-align: center;
font-weight: bold;
}
</style>
<script>
function funcAll(){
var selectAll = document.getElementById('selectAll');
var selectOnes = document.getElementsByClassName('selectOne');
for (var i = 0; i < selectOnes.length; i++) { //循环遍历,把全选框的值赋给每个单选框
selectOnes[i].checked = selectAll.checked;
}
}
function funcOne(){
var selectAll = document.getElementById('selectAll'); //函数作用域,所以得再定义一遍
var selectOnes = document.getElementsByClassName('selectOne');
var isAllChecked = true; //定义一个变量作为桥梁来控制全选按钮
for (var i = 0; i < selectOnes.length; i++) {
if (selectOnes[i].checked === false) {
isAllChecked = false;
break;
}
}
selectAll.checked = isAllChecked;
}
var selectAll = document.getElementById('selectAll'); //函数作用域,所以得再定义一遍
var selectOnes = document.getElementsByClassName('selectOne');
for (var i = 0; i < selectOnes.length; i++) {
selectOnes[i].isAllChecked = !selectOnes[i].checked;
}
function funcOne(){};
var isAllChecked = true; //定义一个变量作为桥梁来控制全选按钮
for (var i = 0; i < selectOnes.length; i++) {
if (selectOnes[i].checked === false) {
isAllChecked = false;
break;
}
}
selectAll.checked = isAllChecked;
}
</script>
</head>
<body>
<div class="wrap">
<table cellspacing="0" cellpadding="14">
<thead>
<tr><th><input type="checkbox" id="selectAll" onclick="funcAll()"></th>
<th>商品</th>
<th>价格</th>
</tr>
</thead>
<tbody id="j_tb">
<tr><td><input type="checkbox" class="selectOne" onclick="funcOne()"></td>
<td>iPhone X</td><td>8000</td>
</tr>
<tr><td><input type="checkbox" class="selectOne" onclick="funcOne()"></td>
<td>iPad pro</td><td>5000</td>
</tr>
<tr><td><input type="checkbox" class="selectOne" onclick="funcOne()"></td>
<td>iPad air</td><td>2000</td>
</tr>
<tr>
<td><input type="checkbox" class="selectOne" onclick="funcOne()"></td>
<td>Apple watch</td><td>2000</td>
</tr>
</tbody>
</table>
<input type="button" id="backwardOption" value="反选" onclick="funcBackward()">
</div>
</body>
</html>