<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>复选</title>
</head>
<body>
<input type="button" name="" value="全选" id="one">
<input type="button" name="" value="反选" id="two">
<input type="checkbox" name="" id="three">
<ul>
<li>
<input type="checkbox" name="li">
</li>
<li>
<input type="checkbox" name="li">
</li>
<li>
<input type="checkbox" name="li">
</li>
<li>
<input type="checkbox" name="li">
</li>
<li>
<input type="checkbox" name="li">
</li>
<li>
<input type="checkbox" name="li">
</li>
</ul>
<script type="text/javascript">
var btn1 = document.getElementById('one')
var btn2 = document.getElementById('two')
var btn3 = document.getElementById('three')
var li = document.getElementsByName('li')
btn1.onclick = function(){
for(var i = 0;i < li.length;i++){
if(li[i].checked ==0 ){
li[i].checked = 1;
}
}
isAll();
}
btn2.onclick = function(){
for(var f = 0;f < li.length;f++){
if (li[f].checked == 1) {
li[f].checked = 0;
}else{
li[f].checked = 1;
}
}
isAll();
}
// 每个checkbox状态修改时判断是否全选
for(var i = 0;i < li.length;i++){
li[i].onchange = function(){
isAll();
}
}
// 判断当前是否全选
function isAll(){
var num = 0;
for(var i = 0;i < li.length;i++){
if(li[i].checked == 1){
num++
}
}
if(num == 6){
btn3.checked = 1;
}else{
btn3.checked = 0;
}
}
</script>
</body>
</html>