html:
html
<div>
<div>
<a href="#">全选</a>
<a href="#">反选</a>
<a href="#">取消</a>
</div>
<label><input type="checkbox" value="苹果" name="box">苹果</label><br>
<label><input type="checkbox" value="草莓" name="box">草莓</label><br>
<label><input type="checkbox" value="橙子" name="box">橙子</label><br>
<label><input type="checkbox" value="苹果" name="box">苹果</label><br>
<label><input type="checkbox" value="西瓜" name="box">西瓜</label>
</div>
script:
javascript
<script>
var inputs = document.querySelectorAll("input")
// 全选
document.querySelectorAll("a")[0].onclick = function () {
for (let i = 0; i < inputs.length; i++) {
inputs[i].checked = true
}
}
// 反选
document.querySelectorAll("a")[1].onclick = function () {
for (let i = 0; i < inputs.length; i++) {
if (inputs[i].checked == true) {
inputs[i].checked = false
} else {
inputs[i].checked = true
}
}
}
// 取消
document.querySelectorAll("a")[2].onclick = function () {
for (let i = 0; i < inputs.length; i++) {
inputs[i].checked = false
}
}
</script>