1.源码如下,可以在下载
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>table</title>
<script type="text/javascript" src="../js/jquery-1.6.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body{margin:0px;padding:0px;}
ul,dl,dt,dd,form,p,h1,h2,h3,h4,h5,h6,blockquote,pre{margin:0;padding:0}
li{list-style:none}
#lstBox{font-size: 12px;}
#lstBox tbody a{text-decoration:none;padding:0 2px;color:#3B5998;width:25px;display:inline-block;}
#lstBox thead tr th.tdLeft{text-align:left;}
#lstBox thead tr th{
padding:12px 2px;
text-align:center;
font-weight: bold;
}
#lstBox tr td {
border-top: 1px solid #EEEEEE;
padding:12px 2px;
text-align:center;
}
#lstBox tr td.tdLeft{
text-align:left;
}
#lstBox tr.altitem td{background: #F9F9F9;}
#lstBox tr.over td{background:#F9FAD1;}/*点击高亮显示*/
#lstBox tr.hvr td{background:#EEF8FF;}/*徘徊显示*/
#lstBox tr.out td{background:#cfeace;}/*徘徊显示*/
.panel{
position:relative;
background: none repeat scroll 0 0 #FFFFFF;
width:48px;
}
.panel .root-item{
width:40px;position:relative;
}
.panel .root-item .sub-list{
width:40px;border:1px solid #CCCCCC;position:absolute;left:4px;top:16px;padding:5px 4px 5px 16px;display:none;
}
.panel .root-item .sub-list li{
margin:0 10px 5px 0;width:35px;
}
.panel .root-item .sub-list li a:hover{
background-color: #4BA88B
}
a{text-decoration:none;color:#3B5998;}
</style>
</head>
<body>
<form action="">
<table id="lstBox" cellspacing="0">
<thead>
<tr>
<th class="tdLeft" style="width: 50px">
<div style="clear:both;">
<ul id="panel" class="panel">
<li class="root-item" >
<a href="javascript:void(0)">
<input type="checkbox" name="allBox" id="checkAll" />
<span id="showSubList">▼</span>
</a>
<ul class="sub-list">
<li id="allselectId" ><a href="javascript:void(0)">全选</a></li>
<li><a href="javascript:void(0)">不选</a></li>
<li><a href="javascript:void(0)">反选</a></li>
</ul>
</li>
</ul>
</div>
</th>
<th class="tdLeft">标题</th>
<th style="width:50px">阅读</th>
<th style="width:50px">评论</th>
<th style="width:70px">评论权限</th>
<th style="width:140px">操作</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tdLeft">
<input type="checkbox" name="box" class="box"/>
</td>
<td class="tdLeft">js+css position:relative;absolute;仿大众点评网首页 左边菜单效果[实例]</td>
<td>93</td>
<td>0</td>
<td>禁止评论</td>
<td><a href="#" >编辑</a>|<a href="#" >置顶</a>|<a href="#" >删除</a></td>
</tr>
<tr class="altitem">
<td class="tdLeft">
<input type="checkbox" name="box" class="box"/>
</td>
<td class="tdLeft">js 完成checkbox 全选与复选[实例]</td>
<td>13</td>
<td>111</td>
<td>禁止评论</td>
<td><a href="#" >编辑</a>|<a href="#" >置顶</a>|<a href="#" >删除</a></td>
</tr>
<tr>
<td class="tdLeft">
<input type="checkbox" name="box" class="box"/>
</td>
<td class="tdLeft">MMC不能打开文件SQLServerEnterpriseManager.MSC的解决方法</td>
<td>93</td>
<td>0</td>
<td>禁止评论</td>
<td><a href="#" >编辑</a>|<a href="#" >置顶</a>|<a href="#" >删除</a></td>
</tr>
<tr class="altitem">
<td class="tdLeft">
<input type="checkbox" name="box" class="box"/>
</td>
<td class="tdLeft">hibernte 批量Update and del[实例]</td>
<td>143</td>
<td>111</td>
<td>禁止评论</td>
<td><a href="#" >编辑</a>|<a href="#" >置顶</a>|<a href="#" >删除</a></td>
</tr>
<tr>
<td class="tdLeft">
<input type="checkbox" name="box" class="box"/>
</td>
<td class="tdLeft">MMC不能打开文件SQLServerEnterpriseManager.MSC的解决方法</td>
<td>93</td>
<td>0</td>
<td>禁止评论</td>
<td><a href="#" >编辑</a>|<a href="#" >置顶</a>|<a href="#" >删除</a></td>
</tr>
<tr class="altitem">
<td class="tdLeft">
<input type="checkbox" name="box" class="box"/>
</td>
<td class="tdLeft">MMC不能打开文件SQLServerEnterpriseManager.MSC的解决方法</td>
<td>93</td>
<td>0</td>
<td>禁止评论</td>
<td><a href="#" >编辑</a>|<a href="#" >置顶</a>|<a href="#" >删除</a></td>
</tr>
<tr>
<td class="tdLeft">
<input type="checkbox" name="box" class="box"/>
</td>
<td class="tdLeft">MMC不能打开文件SQLServerEnterpriseManager.MSC的解决方法</td>
<td>93</td>
<td>0</td>
<td>禁止评论</td>
<td><a href="#" >编辑</a>|<a href="#" >置顶</a>|<a href="#" >删除</a></td>
</tr>
<tr class="altitem">
<td class="tdLeft">
<input type="checkbox" name="box" class="box"/>
</td>
<td class="tdLeft">MMC不能打开文件SQLServerEnterpriseManager.MSC的解决方法</td>
<td>93</td>
<td>0</td>
<td>禁止评论</td>
<td><a href="#" >编辑</a>|<a href="#" >置顶</a>|<a href="#" >删除</a></td>
</tr>
<tr>
<td class="tdLeft">
<input type="checkbox" name="box" class="box"/>
</td>
<td class="tdLeft">MMC不能打开文件SQLServerEnterpriseManager.MSC的解决方法</td>
<td>93</td>
<td>0</td>
<td>禁止评论</td>
<td><a href="#" >编辑</a>|<a href="#" >置顶</a>|<a href="#" >删除</a></td>
</tr>
<tr class="altitem">
<td class="tdLeft">
<input type="checkbox" name="box" class="box"/>
</td>
<td class="tdLeft">MMC不能打开文件SQLServerEnterpriseManager.MSC的解决方法</td>
<td>93</td>
<td>0</td>
<td>禁止评论</td>
<td><a href="#" >编辑</a>|<a href="#" >置顶</a>|<a href="#" >删除</a></td>
</tr>
<tr>
<td class="tdLeft">
<input type="checkbox" name="box" class="box"/>
</td>
<td class="tdLeft">MMC不能打开文件SQLServerEnterpriseManager.MSC的解决方法</td>
<td>93</td>
<td>0</td>
<td>禁止评论</td>
<td>
<a href="#">编辑</a>|
<a href="#">置顶</a>|
<a href="#">删除</a>
</td>
</tr>
<tr class="altitem">
<td class="tdLeft">
<input type="checkbox" name="box" class="box"/>
</td>
<td class="tdLeft">MMC不能打开文件SQLServerEnterpriseManager.MSC的解决方法</td>
<td>93</td>
<td>0</td>
<td>禁止评论</td>
<td>
<a href="#">编辑</a>|
<a href="#">置顶</a>|
<a href="#">删除</a>
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
<script language="JavaScript" type="text/javascript">
$(document).ready(function(){
boxTrFun(); //点击每行,改变背景色 和checkbox状态
checkboxAllFun();//点击全选checkbox 的状态
});
//点击全选checkbox 的状态
function checkboxAllFun(){
$('#checkAll').click(function(){
var allFlag = $(this).is(':checked');
var $checkbox = $('[name=box]:checkbox');
$checkbox.each(function(index){
if(allFlag){
$(this).parents('tr').addClass('over');
}else{
$(this).parents('tr').removeClass('over');
}
$(this)[0].checked = allFlag;
});
});
}
//点击每行,改变背景色 和checkbox状态
function boxTrFun(){
var $lstBoxTr = $('#lstBox > tbody > tr');
$lstBoxTr.click(function(event){
//var flag = $(this).find('td:eq(0) > [name=box]:checkbox').is(':checked');//当前行checkbox状态
//alert(flag);
//已背景色变化 改变checbox的状态
if($(this).hasClass('over')){
$(this).removeClass('over');
$($(this).find('td:eq(0) > [name=box]:checkbox'))[0].checked = false;
}else{
//三目表达式嵌套
$(this).hasClass('hvr') ? $(this).removeClass('hvr') :
($(this).hasClass('out') ? $(this).removeClass('out') : '');
$(this).addClass('over');
$($(this).find('td:eq(0) > [name=box]:checkbox'))[0].checked = true;
}
_each_checkbox();
}).mouseover(function(){
if($(this).hasClass('altitem')){
$(this).addClass('hvr');
}else{
$(this).addClass('out');
}
}).mouseout(function(){
if($(this).hasClass('altitem')){
$(this).removeClass('hvr');
}else{
$(this).removeClass('out');
}
});
}
//对他tbody中每个checbox遍历,设置checkall状态
function _each_checkbox(){
var eachflag = true;
var allSelect = true;
//遍历每个checkbox
$('[name=box]:checkbox').each(function(index){
eachflag = $(this).is(':checked');
if(!eachflag){
allSelect = false;
$('#checkAll')[0].checked=false; //用jquery 自己的方法 存在问题 ,故采用DOM对象
}
});
if(allSelect){//对全选进行设置
$('#checkAll')[0].checked=true;
}
}
//点击全选 不选 反选
$(function(){
$('#lstBox .panel .root-item ul.sub-list li').click(function(){
var index = parseInt($(this).index());
var $checkbox = $('[name=box]:checkbox');
var $checkAll = $('#checkAll');
switch(index){
case 0:
//全选
/*$checkbox.each(function(index){
var eachflag = $(this).is(':checked');
if(!eachflag){
$(this)[0].checked = true;
$(this).parents('tr').addClass('over');
}
});
$checkAll[0].checked=true;*/
inverse_selectAll(true);
break;
case 1:
//不选
inverse_selectAll(false);
/*$checkbox.each(function(index){
var eachflag = $(this).is(':checked');
if(eachflag){
$(this)[0].checked = false;
$checkbox.parents('tr').removeClass('over');
}
});
$checkAll[0].checked=false;*/
break;
case 2:
//反选
var eachflag = true;
var allSelect = true;
//遍历每个checkbox
$checkbox.each(function(index){
eachflag = $(this).is(':checked');
if(eachflag){//选中每个box
$(this)[0].checked=false;
$(this).parents('tr').removeClass('over');
allSelect = false;
}else{
$(this)[0].checked=true;
$(this).parents('tr').addClass('over');
}
});
if(allSelect){//对全选进行设置
$checkAll[0].checked = true;
}else{
$checkAll[0].checked = false;
}
break;
}
});
});
//flag true:全选 false 不选
function inverse_selectAll(flag){
var $checkbox = $('[name=box]:checkbox');
var $checkAll = $('#checkAll');
$checkbox.each(function(index){
var eachflag = $(this).is(':checked');
if(flag){
if(!eachflag){
$(this)[0].checked = flag;
$(this).parents('tr').addClass('over');
}
}else{
if(eachflag){
$(this)[0].checked = flag;
$checkbox.parents('tr').removeClass('over');
}
}
});
$checkAll[0].checked=flag;
}
//显示下拉框的状态
$(function(){
$('#showSubList').click(function(){
$('.sub-list').toggle();
});
});
//点击空白处 对下拉框hide
document.onclick = function(e){
var id;
if (!e) var e = window.event;
if (e.srcElement) {
id = e.srcElement.getAttribute("id")
} else {
id = e.target.getAttribute("id")
}
if(id != 'showSubList'){
$('.sub-list').hide();
}
}
</script>