转自: http://blog.csdn.net/shangmingchao/article/details/49761315

效果图:

HTML中无需添加额外的一列来表示复选框,而是由JS完成,所以正常的表格布局就行了:

  1. <table class="table table-bordered table-hover">
  2. <thead>
  3. <tr class="success">
  4. <th>类别编号</th>
  5. <th>类别名称</th>
  6. <th>类别组</th>
  7. <th>状态</th>
  8. <th>说明</th>
  9. </tr>
  10. </thead>
  11. <tbody>
  12. <tr>
  13. <td>C00001</td>
  14. <td>机车</td>
  15. <td>机车</td>
  16. <td>有效</td>
  17. <td>机车头</td>
  18. </tr>
  19. <tr>
  20. <td>C00002</td>
  21. <td>车厢</td>
  22. <td>机车</td>
  23. <td>有效</td>
  24. <td>载客车厢</td>
  25. </tr>
  26. </tbody>
  27. </table>

重点是JS的实现。复选框很小,不容易点到,所以点击每一行也可以选中该行,并用高亮一些CSS样式表示。点击复选框所在单元格也能选中复选框。下面是完整代码和注释说明:

    1. <!DOCTYPE html>
    2. <html lang="zh-CN">
    3. <head>
    4. <meta charset="utf-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1">
    7. <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    8. <title>表格</title>
    9. <meta name="keywords" content="表格">
    10. <meta name="description" content="这真的是一个表格" />
    11. <meta name="HandheldFriendly" content="True" />
    12. <link rel="shortcut icon" href="img/favicon.ico">
    13. <!-- Bootstrap3.3.5 CSS -->
    14. <link href="css/bootstrap.min.css" rel="stylesheet">
    15. <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    16. <!--[if lt IE 9]>
    17. <script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    18. <script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    19. <![endif]-->
    20. </head>
    21. <body>
    22. <div class="panel-group">
    23. <div class="panel panel-primary">
    24. <div class="panel-heading">
    25. 列表
    26. </div>
    27. <div class="panel-body">
    28. <div class="list-op" id="list_op">
    29. <button type="button" class="btn btn-default btn-sm">
    30. <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
    31. </button>
    32. <button type="button" class="btn btn-default btn-sm">
    33. <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
    34. </button>
    35. <button type="button" class="btn btn-default btn-sm">
    36. <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
    37. </button>
    38. </div>
    39. </div>
    40. <table class="table table-bordered table-hover">
    41. <thead>
    42. <tr class="success">
    43. <th>类别编号</th>
    44. <th>类别名称</th>
    45. <th>类别组</th>
    46. <th>状态</th>
    47. <th>说明</th>
    48. </tr>
    49. </thead>
    50. <tbody>
    51. <tr>
    52. <td>C00001</td>
    53. <td>机车</td>
    54. <td>机车</td>
    55. <td>有效</td>
    56. <td>机车头</td>
    57. </tr>
    58. <tr>
    59. <td>C00002</td>
    60. <td>车厢</td>
    61. <td>机车</td>
    62. <td>有效</td>
    63. <td>载客车厢</td>
    64. </tr>
    65. </tbody>
    66. </table>
    67. <div class="panel-footer">
    68. <nav>
    69. <ul class="pagination pagination-sm">
    70. <li class="disabled">
    71. <a href="#" aria-label="Previous">
    72. <span aria-hidden="true">«</span>
    73. </a>
    74. </li>
    75. <li class="active"><a href="#">1</a></li>
    76. <li><a href="#">2</a></li>
    77. <li><a href="#">3</a></li>
    78. <li><a href="#">4</a></li>
    79. <li><a href="#">5</a></li>
    80. <li>
    81. <a href="#" aria-label="Next">
    82. <span aria-hidden="true">»</span>
    83. </a>
    84. </li>
    85. </ul>
    86. </nav>
    87. </div><!-- end of panel-footer -->
    88. </div><!-- end of panel -->
    89. </div>
    90. <!-- jQuery1.11.3 (necessary for Bo otstrap's JavaScript plugins) -->
    91. <script src="js/jquery-1.11.3.min.js "></script>
    92. <!-- Include all compiled plugins (below), or include individual files as needed -->
    93. <script src="js/bootstrap.min.js "></script>
    94. <script>
    95. $(function(){
    96. function initTableCheckbox() {
    97. var $thr = $('table thead tr');
    98. var $checkAllTh = $('<th><input type="checkbox" id="checkAll" name="checkAll" /></th>');
    99. /*将全选/反选复选框添加到表头最前,即增加一列*/
    100. $thr.prepend($checkAllTh);
    101. /*“全选/反选”复选框*/
    102. var $checkAll = $thr.find('input');
    103. $checkAll.click(function(event){
    104. /*将所有行的选中状态设成全选框的选中状态*/
    105. $tbr.find('input').prop('checked',$(this).prop('checked'));
    106. /*并调整所有选中行的CSS样式*/
    107. if ($(this).prop('checked')) {
    108. $tbr.find('input').parent().parent().addClass('warning');
    109. } else{
    110. $tbr.find('input').parent().parent().removeClass('warning');
    111. }
    112. /*阻止向上冒泡,以防再次触发点击操作*/
    113. event.stopPropagation();
    114. });
    115. /*点击全选框所在单元格时也触发全选框的点击操作*/
    116. $checkAllTh.click(function(){
    117. $(this).find('input').click();
    118. });
    119. var $tbr = $('table tbody tr');
    120. var $checkItemTd = $('<td><input type="checkbox" name="checkItem" /></td>');
    121. /*每一行都在最前面插入一个选中复选框的单元格*/
    122. $tbr.prepend($checkItemTd);
    123. /*点击每一行的选中复选框时*/
    124. $tbr.find('input').click(function(event){
    125. /*调整选中行的CSS样式*/
    126. $(this).parent().parent().toggleClass('warning');
    127. /*如果已经被选中行的行数等于表格的数据行数,将全选框设为选中状态,否则设为未选中状态*/
    128. $checkAll.prop('checked',$tbr.find('input:checked').length == $tbr.length ? true : false);
    129. /*阻止向上冒泡,以防再次触发点击操作*/
    130. event.stopPropagation();
    131. });
    132. /*点击每一行时也触发该行的选中操作*/
    133. $tbr.click(function(){
    134. $(this).find('input').click();
    135. });
    136. }
    137. initTableCheckbox();
    138. });
    139. </script>
    140. </body>
    141. </html>

最新文章

  1. windows下MySQL 忘记初始密码
  2. Hibernate(八)__级联操作、struts+hibernate+接口编程架构
  3. C#微信公众号接口开发实例-高级接口-申请带参数的二维码
  4. android之datepicker控件用法
  5. [原创]大连sap vt 实习生面试经历
  6. Kafka源码中的Producer Record定义
  7. PROPAGATION_REQUIRED事务管理
  8. Socket 错误总结
  9. Windows Phone开发(43):推送通知第一集——Toast推送
  10. xml解析方式之JAXP解析入门
  11. Tiny4412中断介绍
  12. 六十、linux 编程—— I/O 多路复用 select
  13. Neutron local network 学习
  14. 20155205 郝博雅 《网络对抗技术》Exp1 PC平台逆向破解
  15. Python学习 day01打卡
  16. python第一个程序HelloWorld
  17. css 响应式布局
  18. Windbg找出memory leak的一种笨办法
  19. CentOS下mysql安装
  20. 汽车OBD接口定义

热门文章

  1. html分页
  2. sae-服务器php运行环境配置
  3. 解决MySQL查询不区分大小写
  4. Android线程池的使用(未完)
  5. less的使用方法
  6. org
  7. 制作精灵(UI Sprite)
  8. U盘、移动硬盘提示格式化的处理
  9. delphi xe5 android listbox的 TMetropolisUIListBoxItem
  10. static和const