入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

jQuery之实战(checkbox,table)

创建时间:2016-10-16 投稿人: 浏览次数:879
实现功能如下:参考图片 
1 页面加载时效果 
 
2 全选效果 
 
3 取消全选效果 
 
4 反选效果 
 
5 全选奇行效果 
 
6 全选偶行效果 
 
7 点击复选框效果 
 
具体代码如下: 
CSS代码如下: 
Java代码  收藏代码
  1. <style type="text/css">  
  2.            body {  
  3.             font-family: Arial, Helvetica, sans-serif;  
  4.         }     
  5.           
  6.            .listhead  
  7.         {  
  8.           FONT: 15pt "宋体";  
  9.           COLOR: #010778;  
  10.           BACKGROUND-COLOR: #E7EEF7;  
  11.           BORDER-LEFT: #FFFFFF 1px solid;  
  12.           BORDER-TOP: #FFFFFF 1px solid;  
  13.           BORDER-RIGHT: #D9DFE7 1px solid;  
  14.           BORDER-BOTTOM: #D9DFE7 1px solid;  
  15.         }  
  16.           
  17.        </style>     

jQuery代码如下: 
Java代码  收藏代码
  1. <script type="text/javascript" >  
  2.           
  3.             $(function(){  
  4.                 
  5.                 $("#checkAll").click(function(){  
  6.                     /*                   
  7.                     if(this.checked == true){ 
  8.                         $(":checkbox[name=checkUser]").attr("checked",true); 
  9.                     }else{ 
  10.                         $(":checkbox[name=checkUser]").attr("checked",false); 
  11.                     }*/  
  12.                       
  13.                     $("input:checkbox[name=checkUser]").attr("checked",this.checked);  
  14.                     if(this.checked){  
  15.                       
  16.                           $("#tbody>tr").each(function(index){  
  17.                               this.style.backgroundColor = "#cfeace";  
  18.                           });  
  19.                     }else{  
  20.                          $("#tbody>tr").each(function(index){  
  21.                               this.style.backgroundColor = (index % 2 != 1 ? "#F3F3F3" :"#DEE9F7");  
  22.                          });  
  23.                     }  
  24.                                              
  25.                 });   
  26.                   
  27.                 $("#selectAll_1").click(function(){  
  28.                     $("input:checkbox[name=checkUser,checkAll]").attr("checked",true);  
  29.                     $("#tbody>tr").each(function(index){  
  30.                               this.style.backgroundColor = "#cfeace";  
  31.                     });  
  32.                 });       
  33.                   
  34.                 $("#selectAll_2").click(function(){  
  35.                     //$("input:checkbox[name=checkUser,checkAll]").attr("checked",false);  
  36.                     $("input:checkbox[name=checkUser,checkAll]").removeAttr("checked");//取消全选   
  37.                     $("#tbody>tr").each(function(index){  
  38.                         this.style.backgroundColor = (index % 2 != 1 ? "#F3F3F3" :"#DEE9F7");  
  39.                     });  
  40.                 });       
  41.                   
  42.                 $("#selectAll_3").click(function(){  
  43.                       
  44.                     //可用长度来判断是否选定:选中为1,没选为0  
  45.                     //alert($("#checkAll:checked").length);  
  46.                       
  47.                     if ($("#checkAll").is(":checked")) {  
  48.                         //alert("选中了。。");  
  49.                         $("#checkAll").removeAttr("checked");  
  50.                     }else{  
  51.                        // alert("没选中。。");  
  52.                         $("#checkAll").attr("checked",true);  
  53.                     }  
  54.                       
  55.                     /*  
  56.                     $("input:checkbox[name=checkUser]").each(function(index){                        
  57.                         //在jQuery内部用this可提高速度                        
  58.                         if(this.checked){ 
  59.                            this.checked = false;                            
  60.                         }else{ 
  61.                            this.checked = true;                        
  62.                         }  
  63.                     });*/  
  64.                       
  65.                     $("#tbody>tr").each(function(index){  
  66.                       
  67.                           if($(this).find("td:eq(0)>input:checkbox").is(":checked") == true){  
  68.                              $(this).find("td:eq(0)>input:checkbox").attr("checked",false);  
  69.                              this.style.backgroundColor = (index % 2 != 1 ? "#F3F3F3" :"#DEE9F7");  
  70.                           }else{  
  71.                               $(this).find("td:eq(0)>input:checkbox").attr("checked",true);  
  72.                               this.style.backgroundColor = "#cfeace";  
  73.                           }  
  74.                     });  
  75.   
  76.                       
  77.                 });       
  78.                   
  79.                 $("#selectAll_4").click(function(){  
  80.                   
  81.                     $("input:checkbox[name=checkUser]:even").attr("checked",true);                    
  82.                     $("#tbody>tr").each(function(index){                   
  83.                           if($(this).find("td:eq(0)>input:checkbox").is(":checked") == true){                              
  84.                               this.style.backgroundColor = "#cfeace";  
  85.                           }  
  86.                     });  
  87.                       
  88.                 });   
  89.                   
  90.                 $("#selectAll_5").click(function(){  
  91.                     $("input:checkbox[name=checkUser]:odd").attr("checked",true);  
  92.                     $("#tbody>tr").each(function(index){                   
  93.                           if($(this).find("td:eq(0)>input:checkbox").is(":checked") == true){                              
  94.                               this.style.backgroundColor = "#cfeace";  
  95.                           }  
  96.                     });  
  97.                       
  98.                 });    
  99.                   
  100.                 //$("#tbody>tr:even").css("background-color","#F3F3F3");  
  101.                 //$("#tbody>tr:odd").css("background-color","#DEE9F7");    
  102.                   
  103.                 $("#tbody>tr").each(function(index){  
  104.                       
  105.                     this.style.backgroundColor = (index % 2 != 1 ? "#F3F3F3" :"#DEE9F7");  
  106.                       
  107.                     /* 
  108.                     $(this).mouseover(function() { 
  109.                       this.style.backgroundColor = "#EEF8FF"; 
  110.                     }); 
  111.                     $(this).mouseout(function() { 
  112.                       this.style.backgroundColor = (index % 2 != 1 ? "#F3F3F3" :"#DEE9F7"); 
  113.                     });*/  
  114.                       
  115.                     $(this).bind({  
  116.                         mouseover:function(){  
  117.                             this.style.backgroundColor = "#EEF8FF";  
  118.                             if($(this).find("td:eq(0)>input:checkbox").is(":checked") == true){  
  119.                               this.style.backgroundColor = "#cfeace";  
  120.                             }  
  121.                         },mouseout:function(){  
  122.                             this.style.backgroundColor = (index % 2 != 1 ? "#F3F3F3" :"#DEE9F7");  
  123.                             if($(this).find("td:eq(0)>input:checkbox").is(":checked") == true){  
  124.                               this.style.backgroundColor = "#cfeace";  
  125.                             }  
  126.                         },click:function(){  
  127.                             //判断tr的第一个td内的input checkbox是什么状态                             
  128.                             if($(this).find("td:eq(0)>input:checkbox").is(":checked") == true){  
  129.                                 this.style.backgroundColor = "#cfeace";  
  130.                             }else{  
  131.                                 this.style.backgroundColor = (index % 2 != 1 ? "#F3F3F3" :"#DEE9F7");  
  132.                             }                             
  133.                         }                         
  134.                     });  
  135.                       
  136.                 });           
  137.                           
  138.             });   
  139.               
  140.         </script>  

HTML代码如下: 
Java代码  收藏代码
  1. <div style="padding-left:30%;padding-top:5%">  
  2.       <h1>checkbox示例</h1>          
  3.          <table style="width:500px;height:100%;align:center;border: 1px black solid;background: white;" bgcolor=#7E92A8 border=0 cellpadding=1 cellspacing=1 rules="all">  
  4.              <thead>  
  5.                 <tr class="listhead">  
  6.                     <th><input type="checkbox" id="checkAll"  name="checkAll"/></th>  
  7.                     <th>姓名</th>  
  8.                     <th>年龄</th>  
  9.                     <th>性别</th>                       
  10.                 </tr>  
  11.              </thead>  
  12.              <tbody align="center" id="tbody">  
  13.                 <tr id="tr_1">  
  14.                     <td><input type="checkbox" name="checkUser"  value="1"/></td>  
  15.                     <td>张三</td>  
  16.                     <td>25</td>  
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像