js table操作--------- 内容排序

js table操作--------- 内容排序
代码:

< html >
< head >
< meta  http-equiv ="Content-Language"  content ="zh-cn" >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=gb2312" >
< title > 001 </ title >
< script  language =javascript  type =text/javascript >
function paixu(){
var obj=document.getElementsByTagName("table")[0],obj2=obj.cloneNode(true).rows,a=new Array();
while(obj.rows.length!=1)
obj.rows[
1].removeNode(true);
for(var i=1;i<obj2.length;i++)
a[a.length]
=obj2[i];
a.sort(
function(a,b){
return b.cells[1].innerText-a.cells[1].innerText;}

);
for(var i=0;i<a.length;i++)
obj.firstChild.appendChild(a[i]);
}

</ script >
</ head >

< body >

< table  border ="1"  width ="100%"  id ="table1"  cellspacing ="1" >
    
< tr >
        
< td  width ="172" > 代 码 </ td >
        
< td > 名 称 </ td >
    
</ tr >
    
< tr >
        
< td  width ="172" > 001 </ td >
        
< td > 11111 </ td >
    
</ tr >
    
< tr >
        
< td  width ="172" > 002 </ td >
        
< td > 3333 </ td >
    
</ tr >
    
< tr >
        
< td  width ="172" > 00101 </ td >
        
< td > 22222 </ td >
    
</ tr >
    
< tr >
        
< td  width ="172" > 00201 </ td >
        
< td > 4444 </ td >
    
</ tr >
</ table >
    
< p >< button  name ="B3"  onclick ='paixu();' > 排 序 </ button ></ p >

</ body >
</ html >

你可能感兴趣的:(js table操作--------- 内容排序)