首頁  >  文章  >  web前端  >  jquery外掛tablesorter如何實作html表格用法實例詳解

jquery外掛tablesorter如何實作html表格用法實例詳解

伊谢尔伦
伊谢尔伦原創
2017-07-22 09:56:502539瀏覽

tablesort是很好用的jQuery表格排序外掛。 支援多種資料類型排序,會自動辨識表格內容資料類型,使用也非常方便。

使用jQuery tablesort實作html表格方法:

#1. 下載jQuery和tablesort腳本,並在html中引用該腳本檔案:


#
<script type="text/javascript" src="../jquery-latest.js"></script>
<script type="text/javascript" src="../jquery.tablesorter.js"></script>

2. 格式化需要排序的html表格:

a. 指​​定需要排序的表格CCS類別:class="sort-table"
b. 使用thead和tbody標籤來標記表格頭和表格內文
c. 在表格頭thead中需要使用th標籤定義表頭
格式化好的html表格如下所示:


<table border=1 width="800px" class="sort-table">
<thead>
 <tr>
 <th>Date</th>
 <th>Usage</th>
 <th>Cost</th>
 <th>Remain</th>
 </tr>
</thead>
<tbody>
 <tr>
 <td>2008-3-25</td>
 <td>book place @dhgu</td>
 <td>-40</td>
 <td>-40</td>
 </tr>
 <tr>
 <td>2008-4-1</td>
 <td>book place @dhgu</td>
 <td>-40</td>
 <td>-80</td>
 </tr>
</tbody>
</table>

3. 新增jQuery程式碼,啟用html表格排序:
在body中對在第二步驟中指定的排序表格css類別呼叫tablesorter()函數:


<body>
<script type="text/javascript">
 $(document).ready(function() {
 $(".sort-table").tablesorter(); 
 });
</script>

透過上面的3步,jQuery tablesort就可以實現html表格的排序。

以上是jquery外掛tablesorter如何實作html表格用法實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn