js原生实现全选反选
<span>全选:</span><input type="checkbox" id="checkAll">
<div id="content">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
</div>
var checkAll = document.querySelector("#checkAll")
var aCheck = document.querySelector("#content").children
var n = 0
checkAll.onchange = function() {
n = checkAll.checked ? aCheck.length : 0
for (var i = 0; i < aCheck.length; i++) {
aCheck[i].checked = checkAll.checked;
}
}
for (var j = 0; j < aCheck.length; j++) {
aCheck[j].onchange = function() {
this.checked ? n++ : n--
checkAll.checked = n === aCheck.length
}
}