汗....
楼上,如果能的话,小弟自然一开始就写成<td width="xx">的.
但现在需要用脚本动态实现的.呵呵,请各位继续指点!

解决方案 »

  1.   

    你的table本身已经设置了宽度100%,修改也只能改为百分比。如果要用像素,需要开始也使用像素
      

  2.   

    <input type="button" onClick="table1.rows[0].cells[0].style.width=setWidth(100);" name="Submit2" value="测试2">
    <input type="button" onClick="table1.rows[0].cells[0].style.width='10';" name="Submit2" value="测试2">
    <script>
    function setWidth(iwidth)
    {
    var twidth=""+table1.width;
    if(twidth.indexOf("%")!=-1)
      twidth=parseInt(document.body.scrollWidth)*parseInt(twidth)/100; 
    else
      twidth=parseInt(twidth);
    return parseInt(iwidth/twidth*100)+'%'; 
     
    }
    </script>
      

  3.   

    <table width="500" border="1"  cellspacing="0" id="table1">
            <tr>
              <td width="34%">&nbsp;</td>
              <td width="43%">&nbsp;</td>
              <td width="23%">&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
          </table>
    如果是这样呢?
    代码该怎么写?
      

  4.   

    scoutlin(梅川库子) 
    多谢提示.
    但是,能不能直接以象素为单位设置宽度呢?
    只能用间接的方法吗?
      

  5.   

    setWidth(120)
    setWidth("12%")都可以的 
      

  6.   

    可是,
    return parseInt(iwidth/twidth*100)+'%'; 
    最后还是用的百分比.
    能不能用象素呢?
      

  7.   

    小弟是担心,如果这么做,可能会出现一些意想不到的情况.
    比如这样,得到的百分比就会不太准确:
    <table id="table1" width="100%" border="1" cellspacing="0">
      <tr> 
        <td width="25%">&nbsp;</td>
        <td width="25%">&nbsp;</td>
        <td width="25%">&nbsp;</td>
        <td width="25%">&nbsp;</td>
      </tr>
      <tr> 
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr> 
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr> 
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr> 
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <input type="button" name="Button" value="Button" onClick="
    var str='';
    var the=table1.rows[0];
    for(var i=0;i<the.cells.length;i++)
    {
    var a=setWidth(the.cells[i].offsetWidth);
    the.cells[i].width=a;
    str+=(a+'    ');
    }
    "><script>
    function setWidth(iwidth)
    {
    var twidth=""+table1.width;
    if(twidth.indexOf("%")!=-1)
      twidth=parseInt(document.body.scrollWidth)*parseInt(twidth)/100; 
    else
      twidth=parseInt(twidth);
    return parseInt(iwidth/twidth*100)+'%'; 
     
    }
    </script>
      

  8.   

    默认情况下,table采用auto方式的table-layout,IE按照下面的顺序来检测列宽:
    1.By using information in the width property for the col or colGroup element. 
    2.By using information in the width property for the td elements in the first row. 
    3.By dividing the table columns equally, regardless of the size of the content. 参看:http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/tablelayout.asp所以你的代码可以这样:
    <script>
    function set()
    {
    var tb=document.getElementById('table1');
    tb.style.tableLayout='fixed';
    tb.rows[0].cells[0].style.width='10px';
    }
    </script>
    <body>
    <table border="1"  cellspacing="0" id="table1" width=100%>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
          </table>
    <input type="button" onclick="set()" name="Submit2" value="设置宽度">
    </body>
      

  9.   

    table的宽度也用像素就可以解决问题了.