运行代码 缩小
十度
HTML代码
复制 格式化 注释 注释 清空
放大
x
 
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
<head>
3
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4
    <title>jQuery-表格的排序
5
    </title>
6
    <script type="text/javascript" src="http://ku.shouce.ren/libs/jquery/1/jquery1.8.0.min.js">
7
    </script>
8
</head>
9
<body>
10
    <table class="tabSort" width="546" height="300" border="0" align="center" cellpadding="0" cellspacing="0">
11
        <thead>
12
            <tr>
13
                <th scope="col">名称
14
                </th>
15
                <th scope="col">价格
CSS代码
复制 格式化 注释 注释 颜色 清空
放大
xxxxxxxxxx
20
 
1
*{
2
  margin:0px;
3
  padding:0px;
4
}
5
table{
6
  border-collapse:collapse;
7
}
8
table td{
9
  border:1px solid #036;
10
  text-align:center;
11
}
12
thead tr th{
13
  cursor:pointer;
14
  background:#066;
15
  color:#FFFFFF;
16
}
17
thead tr th:hover{
18
  background:#369;
19
}
20
JS代码
复制 格式化 注释 注释 清空
放大
xxxxxxxxxx
64
 
1
$(function(){
2
            //存入点击列的每一个TD的内容;
3
            var aTdCont = [];
4
            //点击列的索引值
5
            var thi = 0
6
            //重新对TR进行排序
7
            var setTrIndex = function(tdIndex){
8
                for(i=0;i<aTdCont.length;i++){
9
                    var trCont = aTdCont[i];
10
                    $("tbody tr").each(function() {
11
                        var thisText = $(this).children("td:eq("+tdIndex+")").text();
12
                        if(thisText == trCont){
13
                            $("tbody").append($(this));
14
                        }
15
                    }
名称
jQuery-表格的排序
分类
表格图层
描述
主要思路:   因为JS有SORT的方法,对数组进行排序,那么通过个方法,我们就会想到数组了。   1.点标表格标头的时候,取出点击的是那一列。即列的索引值。因为下面要进行排序的就是该列。所以我要知道是点的那一列。   2.对表格的数据部分,也就是tbody部分,进行点击的列的取值,把这些值存入到一个数组当中。   3.将存入数据的数组,通过SORT方法进行排序。(这里写了两种,升,或降,因为是点击时要切换排序的方式。第一次降,第二次升,第三降,第四升,依次进行)
收藏