使用jQuery tablesort實(shí)現(xiàn)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標(biāo)簽來標(biāo)記表格頭和表格正文
c. 在表格頭thead中需要使用th標(biāo)簽定義表頭
格式化好的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類調(diào)用tablesorter()函數(shù):
<body> <script type="text/javascript"> $(document).ready(function() { $(".sort-table").tablesorter(); }); </script>
通過上面的3步,jQuery tablesort就可以實(shí)現(xiàn)對html表格的排序。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com