首先來看看實現的效果圖:
HTML中無需添加額外的一列來表示複選框,而是由JS完成,所以正常的表格版面配置就行了:
<table class="table table-bordered table-hover"> <thead> <tr class="success"> <th>類別編號</th> <th>類別名稱</th> <th>類別組</th> <th>狀態</th> <th>說明</th> </tr> </thead> <tbody> <tr> <td>C00001</td> <td>機車</td> <td>機車</td> <td>有效</td> <td>機車頭</td> </tr> <tr> <td>C00002</td> <td>車廂</td> <td>機車</td> <td>有效</td> <td>載客車廂</td> </tr> </tbody></table>
重點是JS的實現。複選框很小,不容易點到,所以點擊每一行也可以選中該行,並用高亮一些CSS樣式表示。點擊複選框所在儲存格也能選中複選框。
下面是完整代碼和注釋說明:
<!DOCTYPE html><html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- 上述3個meta標籤*必須*放在最前面,任何其他內容都*必須*跟隨其後! --> <title>表格</title> <meta name="keywords" content="表格"> <meta name="description" content="這真的是一個表格" /> <meta name="HandheldFriendly" content="True" /> <link rel="shortcut icon" href="img/favicon.ico"> <!-- Bootstrap3.3.5 CSS --> <link href="css/bootstrap.min.css" rel="stylesheet"> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> <!--[if lt IE 9]> <script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script> <![endif]--> </head> <body> <div class="panel-group"> <div class="panel panel-primary"> <div class="panel-heading"> 列表 </div> <div class="panel-body"> <div class="list-op" id="list_op"> <button type="button" class="btn btn-default btn-sm"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增 </button> <button type="button" class="btn btn-default btn-sm"> <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改 </button> <button type="button" class="btn btn-default btn-sm"> <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>刪除 </button> </div> </div> <table class="table table-bordered table-hover"> <thead> <tr class="success"> <th>類別編號</th> <th>類別名稱</th> <th>類別組</th> <th>狀態</th> <th>說明</th> </tr> </thead> <tbody> <tr> <td>C00001</td> <td>機車</td> <td>機車</td> <td>有效</td> <td>機車頭</td> </tr> <tr> <td>C00002</td> <td>車廂</td> <td>機車</td> <td>有效</td> <td>載客車廂</td> </tr> </tbody> </table> <div class="panel-footer"> <nav> <ul class="pagination pagination-sm"> <li class="disabled"> <a href="#" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> <li class="active"><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li> <a href="#" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> </ul> </nav> </div><!-- end of panel-footer --> </div><!-- end of panel --> </div> <!-- jQuery1.11.3 (necessary for Bo otstrap's JavaScript plugins) --> <script src="js/jquery-1.11.3.min.js "></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <script src="js/bootstrap.min.js "></script> <script> $(function(){ function initTableCheckbox() { var $thr = $('table thead tr'); var $checkAllTh = $('<th><input type="checkbox" id="checkAll" name="checkAll" /></th>'); /*將全選/反選複選框添加到表頭最前,即增加一列*/ $thr.prepend($checkAllTh); /*“全選/反選”複選框*/ var $checkAll = $thr.find('input'); $checkAll.click(function(event){ /*將所有行的選中狀態設成全選框的選中狀態*/ $tbr.find('input').prop('checked',$(this).prop('checked')); /*並調整所有選中行的CSS樣式*/ if ($(this).prop('checked')) { $tbr.find('input').parent().parent().addClass('warning'); } else{ $tbr.find('input').parent().parent().removeClass('warning'); } /*阻止向上冒泡,以防再次觸發點擊操作*/ event.stopPropagation(); }); /*點擊全選框所在儲存格時也觸發全選框的點擊操作*/ $checkAllTh.click(function(){ $(this).find('input').click(); }); var $tbr = $('table tbody tr'); var $checkItemTd = $('<td><input type="checkbox" name="checkItem" /></td>'); /*每一行都在最前面插入一個選中複選框的儲存格*/ $tbr.prepend($checkItemTd); /*點擊每一行的選中複選框時*/ $tbr.find('input').click(function(event){ /*調整選中行的CSS樣式*/ $(this).parent().parent().toggleClass('warning'); /*如果已經被選中行的行數等於表格的資料行數,將全選框設為選中狀態,否則設為未選中狀態*/ $checkAll.prop('checked',$tbr.find('input:checked').length == $tbr.length ? true : false); /*阻止向上冒泡,以防再次觸發點擊操作*/ event.stopPropagation(); }); /*點擊每一行時也觸發該行的選中操作*/ $tbr.click(function(){ $(this).find('input').click(); }); } initTableCheckbox(); }); </script> </body></html>
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的協助,如果有疑問大家可以留言留言交流。