可拖拽列头Table

<table width="333" border="1" id="ttt">
  <tr>
    <td >1</td>
    <td >2</td>
    <td >3</td>
    <td>4</td>
  </tr>
  <tr>
    <td> g</td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
</table>

<script>
function fnInit()
{
    for(var i=0;i<ttt.rows.length;i++)
    {
            var _tr = ttt.rows[i];
            for(var j=0;j<_tr.cells.length;j++)
            {
                    var _td = _tr.cells(j);
                    _td.noWrap = true;
                    if(i==0)
                    {                               
                            _tr.attachEvent("onmousedown", fnMousedown);
                            _tr.attachEvent("onmousemove",fnMousemove);
                            _tr.attachEvent("onmouseover",fnMouseover);
                            _tr.attachEvent("onselectstart",fnCancel);
                            window.document.attachEvent("onmouseup", fnMouseup);
                            window.document.attachEvent("onmousemove",fnMouseMove);
                    }
                    else{
                            _td.style.borderRight = "1px solid #C0C0C0";
                            _td.style.borderBottom = "1px solid #C0C0C0";
                    }
            }
   
   
    }
    var _line = window.document.createElement("DIV");
    _line.style.position = "absolute";
    _line.style.backgroundColor="#000000";
    _line.style.width=1;
    window.document.body.appendChild(_line);
    ttt.splitLine = _line;
    ttt.splitLine.style.display = "none";
}
function fnMouseover()
{
    return;
}
function fnMouseMove()
{
    if(!ttt.splitlocked) return;
    fnMousemove();               
}
function fnMousemove()
{
    var oEl = event.srcElement;
    ttt.splitLine.style.left = window.event.x;
    ttt.splitLine.style.top = getTop(ttt);
    ttt.splitLine.style.height = ttt.parentElement.clientHeight;
    if(ttt.splitlocked) return;
    if(!IfSplitLocation(oEl)) return;
}
function fnClick()
{
    var oEl = event.srcElement;
}
function fnMousedown()
{
    var oEl = event.srcElement;
    if(!IfSplitLocation(oEl)) return;
    ttt.splitLine.style.display = "";
    ttt.splitlocked  = true;
    window.document.attachEvent("onselectstart",fnCancel);
}
function fnMouseup()
{
    ttt.splitLine.style.display = "none";
    ttt.splitlocked  = false;
    ttt.document.body.style.cursor='default';
    if(ttt.curResizeTD == null) return;
    var otd = ttt.curResizeTD;
    var otdLeft = getLeft(otd);
    var otdwidth =  ttt.splitLine.style.pixelLeft - otdLeft
    if(otdwidth < 0) return;
    otd.style.width = otdwidth;
    window.document.detachEvent("onselectstart",fnCancel);
}
function IfSplitLocation(oEl)
{
    if(oEl.tagName == "DIV")
            oEl = oEl.parentElement;
    if(oEl.tagName == "TD")
    {
            if(Math.abs(event.offsetX - oEl.clientWidth) <= 5)
            {
                    ttt.curResizeTD = oEl;
                    ttt.document.body.style.cursor='col-resize';
            }
            else if (Math.abs(event.offsetX) <= 5 && oEl.cellIndex>0){
                    if(oEl.cellIndex>0){
                            ttt.curResizeTD = oEl.parentElement.cells(oEl.cellIndex-1);
                            ttt.document.body.style.cursor='col-resize';
                    }
            }
            else{
                    ttt.curResizeTD = null;
                    ttt.document.body.style.cursor='default';
                    return false;
            }
    }
    return true;
}
function getTop(e)
{
    var t=e.offsetTop;
    while(e=e.offsetParent)
    {
        t+=e.offsetTop;
    }
    return t;
}
function getLeft(e)
{
        var l=e.offsetLeft;
        while(e=e.offsetParent)
        {
            l+=e.offsetLeft;
        }
        return l;
}
    function fnCancel()
    {
      window.event.returnValue = false;
      return false;
    }
window.οnlοad=fnInit;
</script> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值