常用代码 原生js全选反选效果
(原创)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input id="btn1" type="button" value="全部选择">
<input id="btn2" type="button" value="全部取消">
<input id="btn3" type="button" value="反选">
<br>
<div id="checkgroup">
<table width="100%" border="0" cellspacing="1" cellpadding="0" bgcolor="#ccc">
<thead>
<tr>
<th class="coll">操作</th>
<th>姓名</th>
<th>成绩</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox">
</td>
<td>张三</td>
<td>800</td>
</tr>
<tr>
<td>
<input type="checkbox">
</td>
<td>张三</td>
<td>800</td>
</tr>
<tr>
<td>
<input type="checkbox">
</td>
<td>张三</td>
<td>800</td>
</tr>
<tr>
<td>
<input type="checkbox">
</td>
<td>张三</td>
<td>800</td>
</tr>
<tr>
<td>
<input type="checkbox">
</td>
<td>张三</td>
<td>800</td>
</tr>
<tr>
<td>
<input type="checkbox">
</td>
<td>张三</td>
<td>800</td>
</tr>
<tr>
<td>
<input type="checkbox">
</td>
<td>张三</td>
<td>800</td>
</tr>
<tr>
<td>
<input type="checkbox">
</td>
<td>张三</td>
<td>800</td>
</tr>
<tr>
<td>
<input type="checkbox">
</td>
<td>张三</td>
<td>800</td>
</tr>
<tr>
<td>
<input type="checkbox">
</td>
<td>张三</td>
<td>800</td>
</tr>
</tbody>
</table>
</div>
<style>
body {
text-align: center;
}
input[type="button"] {
display: inline-block;
background-color: #D55D00;
font-size: 14px;
padding: 5px 20px;
color: #fff;
border-radius: 6px;
border: none;
margin-right: 10px;
}
table {
margin-top: 20px;
}
table th {
font-size: 14px;
background-color: #e5e5e5;
padding: 10px;
}
table td {
background-color: #fff;
padding: 10px;
font-size: 14px;
}
table .coll {
width: 1px;
white-space: nowrap;
}
</style>
<script>
window.onload = function () {
var btn1 = document.getElementById("btn1");
var btn2 = document.getElementById("btn2");
var btn3 = document.getElementById("btn3");
var div1 = document.getElementById("checkgroup");
var odiv = div1.getElementsByTagName("input");
btn1.onclick = function () {
for (var i = 0; i < odiv.length; i++) {
odiv[i].checked = true;
}
};
btn2.onclick = function () {
for (var i = 0; i < odiv.length; i++) {
odiv[i].checked = false;
}
};
btn3.onclick = function () {
for (var i = 0; i < odiv.length; i++) {
if (odiv[i].checked == true) {
odiv[i].checked = false;
} else {
odiv[i].checked = true;
}
}
};
}
</script>
</body>
</html>