首页 新闻 会员 周边 捐助

对表格进行升降排序:为什么我只能点击排序一次?

0
悬赏园豆:200 [已解决问题] 解决于 2009-04-29 13:44

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type ="text/javascript" >
        function sortTable(sTableID,iCol) {

            var oTable = document.getElementById(sTableID);
            var oTBody = oTable.tBodies[0];
            var colDataRows = oTBody.rows;
            var aTRs = new Array;
            for (var i = 0; i < colDataRows.length; i++) {
                aTRs[i] = colDataRows[i];
            }
            if (oTable.sortCol == iCol) {
                aTRs.reverse();
            } else {
                aTRs.sort(generateCompareTRs(iCol));
            }
            var Fragment = document.createDocumentFragment();
            for (var i = 0; i < aTRs.length; i++) {
                Fragment.appendChild(aTRs[i]);
            }
            oTable.appendChild(Fragment);
            oTable.sortCol = iCol;
        }
        function generateCompareTRs(iCol) {
            return function comparesTRs(tv1, tv2) {
                var sv1 = tv1.cells[iCol ].firstChild.nodeValue;
                var sv2 = tv2.cells[iCol ].firstChild.nodeValue;
                return sv1.localeCompare(sv2);
            }
        }
       
       
    </script>
</head>
<body>
    <form id="form1" method ="post" action ="" >
    <table border ="1" id ="tblSort" >
   <thead >
   <tr >
   <th onclick ="sortTable('tblSort',0)" style ="cursor :pointer ">Last Name</th>
   <th onclick ="sortTable('tblSort',1)" style ="cursor :pointer ">First Name</th>
   </tr>
   </thead>
   <tbody >
   <tr><td>Smith</td><td>John</td></tr>
   <tr ><td>Johnson</td><td >Betty</td></tr>
   <tr ><td >Henderson</td><td >Nathan</td></tr>
   <tr><td>Williams</td><td >James</td></tr>
   <tr><td>Gilliam</td><td>Michael</td></tr>
   <tr><td>Walker</td><td >Matthew</td></tr>
   </tbody>
    </table>
    </form>
</body>
</html>

菜鸟001的主页 菜鸟001 | 初学一级 | 园豆:0
提问于:2009-04-29 11:51
< >
分享
最佳答案
0
黑白之间 | 小虾三级 |园豆:859 | 2009-04-29 13:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册