<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="button" value="全選" id="all">
<input type="button" value="不選" id="no">
<input type="button" value="反選" id="rev">
<div id="box">
<input type="checkbox" name="" id="">
<input type="checkbox" name="" id="">
<input type="checkbox" name="" id="">
</div>
<script>
window.onload = function() {
var oAll = document.getElementById('all');
var oNo = document.getElementById('no');
var oRev = document.getElementById('rev');
var oBox = document.getElementById('box');
var aInp = oBox.getElementsByTagName('input');
oAll.onclick = function() {
for (var i = 0; i < aInp.length; i++) {
aInp[i].checked = true;
}
};
oNo.onclick = function() {
for (var i = 0; i < aInp.length; i++) {
aInp[i].checked = false;
}
};
oRev.onclick = function() {
for (var i = 0; i < aInp.length; i++) {
aInp[i].checked = !aInp[i].checked;
}
}
}
</script>
</body>
</html>
運行結果