后来使用这样的方法来改变标头的宽度。
function set_width(table1,table2)
{var allstr=0; for(var i=0; i<table1.rows[0].cells.length; i++)
{
allstr=allstr+table1.rows[0].cells[i].innerText

}for(var i=0; i<table1.rows[0].cells.length; i++)
{
table2.rows[0].cells[i].width=(100*table1.rows[0].cells[i].innerText.length/allstr.length)+"%"

}}
可是这样没办法得到哪列最宽,而且有的单元格内还有多行数据,但宽度不一定最长,这样就更出现误差了。

解决方案 »

  1.   

    好像是梅花雪的代码,贴上来参考吧<html><head><title></title></head><body>
    <style>
    div.DataGrid
    {
      overflow: auto;
      height: 160;
      width: 400;
    }
    </style>
    <SCRIPT LANGUAGE="JavaScript">
    function fixupFirstRow(tab)
    {
      var div = tab.parentNode;
      if(div.className.toLowerCase() == "datagrid")
      {
        tab.rows[0].style.zIndex = "1";
        tab.rows[0].style.position = "relative";
        div.onscroll = function()
        {
          var tr = tab.rows[0];
          tr.style.top = this.scrollTop - (this.scrollTop==0 ? 1 : 2);
          tr.style.left = -1;
        }
      }
    }
    window.onload = function()
    {
      var tab = document.getElementById("content");
      if(tab) fixupFirstRow(tab);
    }
    </SCRIPT><div class=DataGrid>
      <table id=content width=600 border=1 class=table borderColor='#DAEAF5' align=center>
        <tr bgcolor=yellow><td>第一列</td><td>第二列</td><td>第三列</td><td>第四列</td></tr>
        <tr><td>1111111</td><td>2222222</td><td>3333333</td><td>444444</td></tr>
        <tr><td>1111111</td><td>2222222</td><td>3333333</td><td>444444</td></tr>
        <tr><td>1111111</td><td>2222222</td><td>3333333</td><td>444444</td></tr>
        <tr><td>1111111</td><td>2222222</td><td>3333333</td><td>444444</td></tr>
        <tr><td>1111111</td><td>2222222</td><td>3333333</td><td>444444</td></tr>
        <tr><td>1111111</td><td>2222222</td><td>3333333</td><td>444444</td></tr>
        <tr><td>1111111</td><td>2222222</td><td>3333333</td><td>444444</td></tr>
        <tr><td>1111111</td><td>2222222</td><td>3333333</td><td>444444</td></tr>
      </table>
    </div>
    </body>
    </html>
      

  2.   

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>JK:支持民族工?,尽量少?X?</title>
    <style>
    .fixedHeaderTr 

    position:relative; 
    top:expression(this.offsetParent.scrollTop); 
    }; .mainDiv 

    overflow:auto; 
    scrollbar-face-color:9999ff;
    height:expression((document.body.clientHeight-this.offsetTop-20>this.children[0].offsetHeight)?(this.children[0].offsetHeight+20) : (document.body.clientHeight-this.offsetTop-20)); 
    width:expression(document.body.clientWidth-20); 
    }
    </style>
    </head> <body>
    部分代?来自:<br>http://blog.csdn.net/wu_yongcai/archive/2004/11/21/189816.aspx
    <br>
      <div class="mainDiv">
         <table width="100%" border=1 cellspacing=0 style="margin-top:-2px" >
         <TR class="fixedHeaderTr" style="">
         <TD nowrap rowspan=2 style="position:relative;background:navy;color:white;">Header A </TD>
         <TD nowrap colspan=2 style="background:navy;color:white;" > Header B C</TD>     </TR>
         <TR class="fixedHeaderTr">
         <TD nowrap style="background:navy;color:white;">Header B</TD>
         <TD nowrap style="background:navy;color:white;">Header C</TD>
         </TR>
         <TR>
         <TD>A</TD>
         <TD>B</TD>
         <TD nowrap >
           C部分代?来自:<br>
           http://blog.csdn.net/wu_yongcai/archive/2004/11/21/189816.aspxC?暘????丗
    http://blog.csdn.net/wu_yongcai/archive/2004/11/21/189816.aspxC?暘????丗
    http://blog.csdn.net/wu_yongcai/archive/2004/11/21/189816.aspxC?暘????丗
    http://blog.csdn.net/wu_yongcai/archive/2004/11/21/189816.aspxC?暘????丗
    http://blog.csdn.net/wu_yongcai/archive/2004/11/21/189816.aspx</TD>
         </TR>
         <TR>
         <TD>A</TD>
         <TD>B</TD>
         <TD>C</TD>
         </TR>
         <TR>
         <TD>A</TD>
         <TD>B</TD>
         <TD>C</TD>
         </TR>
         <TR>
         <TD>A</TD>
         <TD>B</TD>
         <TD>C</TD>
         </TR>
         <TR>
         <TD>A</TD>
         <TD>B</TD>
         <TD>C</TD>
         </TR>
         <TR>
         <TD>A</TD>
         <TD>B</TD>
         <TD>C</TD>
         </TR>
         <TR>
         <TD>A</TD>
         <TD>B</TD>
         <TD>C</TD>
         </TR>
         <TR>
         <TD>A</TD>
         <TD>B</TD>
         <TD>C</TD>
         </TR>
         <TR>
         <TD>A</TD>
         <TD>B</TD>
         <TD>C</TD>
         </TR>
         </table>
      </div>
     </body>
    </html>JK同学的
      

  3.   

    原来还是样式表和dhtml属性不太清楚啊,惭愧。多谢指教。
      

  4.   

    http://blog.csdn.net/wu_yongcai/archive/2004/11/21/189816.aspx
    这个好强mk