<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript" type="text/javascript">
var flag=false;
function Table(n){
this.n=n;
this.t;this.b;this.r;this.w; this.a=[]; this.l=0;
}
Table.prototype.init=function(){
this.t=document.getElementById(this.n);
this.b=this.t.getElementsByTagName("tbody")[0];
this.r=this.b.rows; var l=this.r.length;
for(var i=0;i<l;i++){
if(i==0){
var c=this.r[i].cells;this.w=c.length;
for(var x=0;x<this.w;x++){
var th=c[x];
//排序
if(c[x].className!='nosort'){
c[x].className='head';
c[x].onclick=new Function('this.work(th.cellIndex)');//new Function("work(th)")//new Function('.work(this.cellIndex)');//new Function(this.n+'.work(this.cellIndex)')
}
//添加事件
this.addEvent(th,"mousemove",function(event){
event=window.event||event;
var td=event.srcElement||event.target,tblParent=td.parentElement||td.parentNode;
if(event.offsetX>td.offsetWidth-10)
td.style.cursor='w-resize';
else
td.style.cursor='default';
if(flag)
{
if(td.oldWidth+(event.x-td.oldX)>0)
td.width=td.oldWidth+(event.x-td.oldX);
td.style.width=td.width;
td.style.cursor='w-resize';

table=td;
while(table.tagName!='TABLE') table=table.parentElement;
table.width=(td.tableWidth||td.width)+(td.offsetWidth-td.oldWidth);
table.style.width=table.width;
}
});
this.addEvent(th,"mousedown",function(event){
event=event||window.event;
var td=event.srcElement||event.target,tblParent=td.parentElement||td.parentNode;
if(event.offsetX>td.offsetWidth-10)
{
flag=true;
td.oldX=event.x;
td.oldWidth=td.offsetWidth;
table=td;while(table.tagName!='TABLE')table=table.parentElement;
td.tableWidth=table.offsetWidth;
}
});
this.addEvent(th,"mouseup",function(event){
flag=false;
});
this.addEvent(th,"",function(event){
flag=false;
});
}
}else{
this.a[i-1]={}; this.l++
}
}
}Table.prototype.work=function(y){
this.b=this.t.getElementsByTagName('tbody')[0]; this.r=this.b.rows;
var x=this.r[0].cells[y],i;
for(i=0;i<this.l;i++){
this.a[i].o=i+1; var v=this.r[i+1].cells[y].firstChild;
this.a[i].value=(v!=null)?v.nodeValue:''
}
for(i=0;i<this.w;i++){
var c=this.r[0].cells[i];
if(c.className!='nosort'){c.className='head'}
}
if(this.p==y){
this.a.reverse(); x.className=(this.d)?'asc':'desc';
this.d=(this.d)?false:true
}else{
this.p=y; this.a.sort(function(a,b){return false;}); x.className='asc'; this.d=false
}
var n=document.createElement('tbody');
n.appendChild(this.r[0]);
for(i=0;i<this.l;i++){
var r=this.r[this.a[i].o-1].cloneNode(true);
n.appendChild(r); r.className=(i%2==0)?'even':'odd'
}
this.t.replaceChild(n,this.b)
}
function compare(f,c){
f=f.value,c=c.value;
var i=parseFloat(f.replace(/(\$|\,)/g,'')),n=parseFloat(c.replace(/(\$|\,)/g,''));
if(!isNaN(i)&&!isNaN(n)){f=i,c=n}
return (f>c?1:(f<c?-1:0))
}
/*
Table.prototype.compare=function(f,c){
f=f.value,c=c.value;
var i=parseFloat(f.replace(/(\$|\,)/g,'')),n=parseFloat(c.replace(/(\$|\,)/g,''));
if(!isNaN(i)&&!isNaN(n)){f=i,c=n}
return (f>c?1:(f<c?-1:0))
}*/
function findPos(obj){        //取得本元素的绝对坐标
    var x=obj.offsetLeft,    y=obj.offsetTop;
    while(obj=obj.offsetParent){x += obj.offsetLeft;y += obj.offsetTop;}
    this.intX=x;    this.intY=y;
    return this
}
Table.prototype.addEvent=function(obj, type, fn){
var d=document;
var browser=function(s){return navigator.userAgent.toLowerCase().indexOf(s)!=-1}; //浏览器类型判断
var isOpera=browser('opera'),isIE=browser('msie')!=-1&&(d.all&&!isOpera);
if(isIE){
obj.attachEvent("on"+type,fn);
}else{
obj.addEventListener(type,fn,false)
}
}
var table=new Table("myTable");
table.addEvent(window,"load",function(){table.init();});</script>
</head><body>
<table id="myTable" border="1" cellpadding="0" cellspacing="0" class="tablesorter">
    <tr>
        <th>Last Name</th>
        <th>First Name</th>
        <th>Email</th>
        <th>Due</th>
        <th>Web Site</th>
   </tr>
    <tr>
        <td>Smith</td>
        <td>John</td>
        <td>[email protected]</td>
        <td>$50.00</td>
        <td>http://www.jsmith.com</td>
   </tr>
   <tr>
        <td>Bach</td>
        <td>Frank</td>
        <td>[email protected]</td>
        <td>$50.00</td>
        <td>http://www.frank.com</td>
  </tr>
  <tr>
        <td>Doe</td>
        <td>Jason</td>
        <td>[email protected]</td>
        <td>$100.00</td>
        <td>http://www.jdoe.com</td>
 </tr>
 <tr>
     <td>Conway</td>
     <td>Tim</td>
     <td>[email protected]</td>
     <td>$50.00</td>
     <td>http://www.timconway.com</td>
</tr>
</table> 
</body>
</html>

解决方案 »

  1.   

    里面 c[x].onclick=new Function('this.work(th.cellIndex)');
    Table.prototype.work=function(y){
    if(td.oldWidth+(event.x-td.oldX)>0)
                                td.width=td.oldWidth+(event.x-td.oldX);
                            td.style.width=td.width;
                            td.style.cursor='w-resize';
                            
                            table=td;
                            while(table.tagName!='TABLE') table=table.parentElement;
                            table.width=(td.tableWidth||td.width)+(td.offsetWidth-td.oldWidth);
                            table.style.width=table.width;
    写法有问题,能把整个js文件给调通