/**********************************************************
PowerTable:对网上流传的一种表格点击排序的函数的对象化封装。
原作者:迭名
当前版本:0.1
封装:廖建祥 2006-8-14
**********************************************************/
function PowerTable() {
 var pt = new Object();
 
 pt.Create = function () { //原生态
  pt.Main_Tab = null;        
  pt.cur_row = null;       
  pt.cur_col = null;        
  pt.Org_con = "";          
  pt.sort_col = null;    
  pt.show_col = false;   
  pt.cur_fc = "black";  
  pt.style =  "FONT-SIZE: 11pt;color:red;" ;  
 }
 
 pt.Create();
 
 pt.Set = function (table) { //实例与网页上的表格结合
  pt.cur_row = null ;                                  
  pt.cur_col = null ;                                 
  pt.sort_col = null ;                                  
  pt.Main_Tab = table;                            
  pt.read_def(pt.Main_Tab);                                       
  pt.Main_Tab.onclick = function () {pt.OnClick()};                            
  pt.Org_con = pt.Main_Tab.outerHTML;                     
  pt.arrowUp = document.createElement("SPAN");                 
  pt.arrowUp.innerHTML = "↑" ;                              
  pt.arrowUp.style.cssText =  pt.style; 
  pt.arrowDown = document.createElement("SPAN");               
  pt.arrowDown.innerHTML = "↓" ;                              
  pt.arrowDown.style.cssText = pt.style;     
 }
 
 pt.read_def = function (the_table){                                                                   
  for(var i=0;i<the_table.rows.length;i++){                                                   
   for(var j=0;j<the_table.rows[i].cells.length;j++){                                      
    with(the_table.rows[i]){                                                            
     cells[j].oBgc = cells[j].currentStyle.backgroundColor;                           
     cells[j].oFc  = cells[j].currentStyle.color;                                     
    }              
   }                  
  }                      
 }   pt.sort_tab = function (the_tab,col,mode){                                                    
  var tab_arr = new Array();                                                                   
  var i;                  
  var start=new Date();     
  if(the_tab.rows.length<=2)                                                                  
   return ;            
  for(i=1;i<the_tab.rows.length-1;i++){                                                       
   tab_arr.push(new Array(parseInt(the_tab.rows[i].cells[col].innerText.toLowerCase()),the_tab.rows[i]));                                                        
  }                      
  var lastLine=the_tab.rows[the_tab.rows.length-1];                                           
  function SortArr(mode) {                                                                    
   return function (arr1, arr2){                                                           
    var flag       
    var a,b        
    a = arr1[0]    
    b = arr2[0]    
    if(/^(\\+|-)?\\d+($|\\.\\d+$)/.test(a) && /^(\\+|-)?\\d+($|\\.\\d+$)/.test(b)){     
     a=eval(a)  
     b=eval(b)  
     flag=mode?(a>b?1:(a<b?-1:0)):(a<b?1:(a>b?-1:0))                                 
    }else{         
     a=a.toString()                                                                  
     b=b.toString()                                                                  
     flag=mode?(a>b?1:(a<b?-1:0)):(a<b?1:(a>b?-1:0))                                 
    }              
    return flag    
   }                  
  }                     
  tab_arr.sort(SortArr(mode)) ;                                                            
  for(i=0;i<tab_arr.length;i++){                                                              
   the_tab.lastChild.appendChild(tab_arr[i][1]) ;                                           
  }                     
  the_tab.lastChild.appendChild(lastLine);                                                    
 }
 
 pt.OnClick = function () {
  function get_Element(the_ele,the_tag){                          
   the_tag = the_tag.toLowerCase();                             
   if(the_ele.tagName.toLowerCase()==the_tag)return the_ele;    
   while(the_ele=the_ele.offsetParent){                        
    if(the_ele.tagName.toLowerCase()==the_tag)return the_ele;
   }                                                           
   return null;                                                
  }
  
  event.cancelBubble=true;
  var the_obj = event.srcElement;                                                            
  var i = 0 ,j = 0 ;   
  if(the_obj.tagName.toLowerCase() != "table" && the_obj.tagName.toLowerCase() != "tbody" && the_obj.tagName.toLowerCase() != "tr"){                                
   var the_td = get_Element(the_obj,"td") ;                                              
   if(the_td==null) return;                                                                 
   var the_tr = the_td.parentElement;                                                      
   var the_table= get_Element(the_td,"table");                                           
   if(the_table.rows.length<=2)                                                            
    return ;        
   var i = 0    
   pt.cur_row = the_tr.rowIndex;                                                               
   pt.cur_col = the_td.cellIndex;                                                              
   if(pt.cur_row!=0){    
    for(i=0;i<the_tr.cells.length;i++){                                                 
     with(the_tr.cells[i]){                                                          
      style.color=pt.cur_fc;                                                          
     }          
    }              
   }else{             
    if(pt.show_col){  
     for(i=1;i<the_table.rows.length;i++){                                           
      with(the_table.rows[i].cells[pt.cur_col]){                                     
       style.color=pt.cur_fc;                                                      
      }      
     }          
    }              
                   
    the_td.mode = !the_td.mode;                                                          
    if(pt.sort_col!=null){                                                                 
     with(the_table.rows[0].cells[pt.sort_col])                                         
      removeChild(lastChild);                                                      
    }              
    with(the_table.rows[0].cells[pt.cur_col])                                              
     appendChild(the_td.mode?pt.arrowUp:pt.arrowDown);                                      
    pt.sort_tab(the_table,pt.cur_col,the_td.mode);                                             
    pt.sort_col=pt.cur_col;                                                                    
   }                  
  }      
 }
 
 return pt; 
}      
 
<meta name="robots" content="noindex, nofollow" />  <htmL>
<head>
<title>PowerTable</title>
<script src=PowerTable.js></script>
</head>
<body>
<table border="1" align="center" bordercolor="#808080" id=table_id>
<tr>
<td width="60" height="30">第1列</td>
<td width="60">第2列</td>
</tr>
<tr>
<td height="30">3</td>
<td>4</td>
</tr>
<tr>
<td height="30">2</td>
<td>5</td>
</tr>
<tr>
<td height="30">1</td>
<td>6</td>
</tr>
<tr>
<td height="30">合计</td>
<td>15</td>
</tr>
</table>
<script>
 var my_power_table = new PowerTable();
 my_power_table.Set(table_id); 
</script>
</body>
</html>
我copy 收集的  呵呵