jq表格全反选实例

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.backcolor{
background: #8A2BE2;
}
</style>
<script src="js/jquery-1.4.1.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$(function(){
//单选
$("#tab tbody tr input").click(function(){
$(this).parent().parent().toggleClass("backcolor");
});
//全选、全不选
$("#selAll").click(function(){
$("#selAgainst").attr("checked","");
if($(this).attr("checked")){
$("#tab tbody tr").addClass("backcolor");
$("#tab tbody tr input[type=checkbox]").attr("checked","checked")
}else{
$("#tab tbody tr").removeClass("backcolor");
$("#tab tbody tr input[type=checkbox]").removeAttr("checked")
}
});
//反选
$("#selAgainst").click(function(){
$("#selAll").attr("checked","");
$("#tab tbody tr").toggleClass("backcolor");
$("#tab tbody tr input[type=checkbox]").each(function(){
if($(this).attr("checked")!=true){
$(this).attr("checked","checked");
}else{
$(this).removeAttr("checked")
}
});
});
});
</script>
</head>
<body>
<table border="1" width="350px" id="tab">
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>班级</th>
</tr>
<tr>
<td colspan="4"><input type="checkbox" id="selAll"/>全选/全不选
<input type="checkbox" id="selAgainst"/>反选
</td>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox"/>s001</td>
<td>小白</td>
<td>女</td>
<td>t111</td>
</tr>
<tr>
<td><input type="checkbox"/>s002</td>
<td>小黑</td>
<td>男</td>
<td>t111</td>
</tr>
<tr>
<td><input type="checkbox"/>s003</td>
<td>小强</td>
<td>男</td>
<td>t112</td>
</tr>
<tr>
<td><input type="checkbox"/>s004</td>
<td>张三</td>
<td>男</td>
<td>t113</td>
</tr>
<tr>
<td><input type="checkbox"/>s005</td>
<td>小兰</td>
<td>女</td>
<td>t113</td>
</tr>
</tbody>
</table>
</body>
</html>
posted @ 2017-07-14 18:11  Sun_Song  阅读(424)  评论(0编辑  收藏  举报