<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-type" content="text/html" charset="utf-8">
<title>sort table</title>
<style>
*{
margin:0px;
padding:0px;
}
body{
background:#ccc;
}
table{
width:350px;
margin:0 auto;
background-color:#eee;
}
table th{
cursor:hand;
padding:5px 0;
background-color:#999;
}
table td{
background-color:#fff;
font-size:16px;
font-weight:normal;
text-align:center;
line-height:30px;
}
</style>
<script language="javascript">
function sortCells(type){
var tbs=document.getElementsByTagName("table")[0];
var arr=[];
var arr2=[];
for(var i=1;i<tbs.rows.length;i++){
var text=tbs.rows[i].cells[type].innerText;
arr.push(text);
arr2[text]=i;
}
if(type==0){
arr.sort(function(a,b){return a-b});
}else{
arr.sort();
}
var temp=""; for(var j=1;j<tbs.rows.length;j++){
temp=tbs.rows[j].cells[0].innerText;
tbs.rows[j].cells[0].innerText=tbs.rows[arr2[arr[j-1]]].cells[0].innerText;
tbs.rows[arr2[arr[j-1]]].cells[0].innerText=temp; temp=tbs.rows[j].cells[1].innerText;
tbs.rows[j].cells[1].innerText=tbs.rows[arr2[arr[j-1]]].cells[1].innerText;
tbs.rows[arr2[arr[j-1]]].cells[1].innerText=temp; temp=tbs.rows[j].cells[2].innerText;
tbs.rows[j].cells[2].innerText=tbs.rows[arr2[arr[j-1]]].cells[2].innerText;
tbs.rows[arr2[arr[j-1]]].cells[2].innerText=temp;
// console.log(arr2);
for(var i=1;i<tbs.rows.length;i++){
var text=tbs.rows[i].cells[type].innerText;
arr2[text]=i;
}
}
}
</script>
</head>
<body>
<center>sort table</center>
<table border="0">
<tr>
<th onclick="sortCells(0);">序号</th>
<th onclick="sortCells(1);">姓名</th>
<th onclick="sortCells(2);">日期</th>
</tr>
<tr>
<td>2</td>
<td>BB</td>
<td>2015-09-12</td>
</tr>
<tr>
<td>3</td>
<td>CC</td>
<td>2015-07-12</td>
</tr>
<tr>
<td>1</td>
<td>AA</td>
<td>2015-09-11</td>
</tr>
<tr>
<td>4</td>
<td>DD</td>
<td>2015-06-12</td>
</tr> </table>
</body>
</html>

最新文章

  1. http status 状态码汇总
  2. js点击按钮倒计时setTimeout和setInterval
  3. Linux下cutecom使用USB转串口线
  4. LintCode &quot;Longest Increasing Continuous subsequence II&quot; !!
  5. 【PL/SQL】异常处理:
  6. I/O系统,多线程、图形用户界面编程
  7. iOS动画一点也不神秘————你是喜欢看幻灯片?还是看高清电影?
  8. POJ3318【随机化算法挺有意思】
  9. 新版SDK自己主动加入PlaceholderFragment的思考
  10. BootStrap dateRangePicker时间范围控件
  11. (转)Bat Command
  12. JAVASE高级2
  13. 在Java Web项目中添加定时任务
  14. Count on a tree
  15. vue项目中获取cdn域名插件
  16. permute
  17. 每周分享之cookie详解
  18. HTML常用标签及属性
  19. Locust性能测试
  20. xhr文件类型说明

热门文章

  1. 【PostgreSQL】PostgreSQL的安装
  2. Django数据导入
  3. Expert 诊断优化系列-------------针对重点语句调索引
  4. Box-sizing:小身材,大拳头!
  5. RxJava 备注
  6. C#中使用Socket实现简单Web服务器
  7. Redis高可用分布式内部交流(九)
  8. JS实战 &#183; 级联菜单选择省份和城市(两种)
  9. Java中instanceof和isInstance区别详解
  10. struts1二:基本环境搭建