<!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>
<body>
全选:<input type="checkbox" class="all">
<hr>
单选:<input type="checkbox" class="other"><br>
单选:<input type="checkbox" class="other"><br>
单选:<input type="checkbox" class="other"><br>
单选:<input type="checkbox" class="other"><br>
单选:<input type="checkbox" class="other"><br>
<script>
var oAll = document.querySelector('.all');
var oOthers = [...document.querySelectorAll('.other')];
console.log(oOthers);
oAll.onclick = function () {
if (oAll.checked === true) {
oOthers.forEach(function (value) {
value.checked = true;
})
} else {
oOthers.forEach(function (value) {
value.checked = false;
})
}
}
oAll.onclick = function(){
oOthers.forEach(function(value){
value.checked = oAll.checked;
})
}
oOthers.forEach(function(value){
value.onclick = function(){
oAll.checked = oOthers.every(function(value){
console.log(oAll.checked);
return value.checked;
})
}
})
</script>
</body>
</html>