日期:2014-05-16 浏览次数:20873 次
<html>
<script language="javascript">
var sorttype=1; //desc
function sortByCol(colNum){
//排序
var parNode=document.getElementById("listtable"); //定位到table上
for(var i=0; i<parNode.rows.length-1; i++)
{
for(var n=1; n<parNode.rows.length-1-i; n++){
var ifrom=parNode.rows.length-n;
var ito=parNode.rows.length-n-1;
if(sorttype==1){
if(parNode.rows[ifrom].cells[colNum].childNodes[0].data > parNode.rows[ito].cells[colNum].childNodes[0].data){
parNode.moveRow(ifrom,ito); //from-to
}
}else{
if(parNode.rows[ifrom].cells[colNum].childNodes[0].data < parNode.rows[ito].cells[colNum].childNodes[0].data){
parNode.moveRow(ifrom,ito); //from-to
}
}
}
}
sorttype=0 - sorttype; //asc
}
</script>
<body>
<table id="listtable" width="100%" border="1" cellpadding="0" cellspacing="1">
<tr><th onclick="sortByCol(0);">序号</th><th onclick="sortByCol(1);">姓名</th>
<th onclick="sortByCol(2);">年龄</th><th onclick="sortByCol(3);">班级</th><th onclick="sortByCol(4);">成绩</th></tr>
<tr><td>1</td><td>ZXE</td><td>56</td><td>1</td><td>13</td></tr>
<tr><td>2</td><td>SCHB</td><td>34</td><td>3</td><td>23</td></tr>
<tr><td>3</td><td>WE</td><td>23</td><td>3</td><td>34</td></tr>
<tr><td>4</td><td>SQD</td><td>34</td><td>2</td><td>76</td></tr>
<tr><td>5</td><td>EDE</td><td>67</td><td>1</td><td>34</td></tr>
<tr><td>6</td><td>BD</td><td>32</td><td>2</td><td>78</td></tr>
<tr><td>7</td><td>AA</td><td>23</td><td>1</td><td>34</td></tr>
</body>
</html>