默认情况下,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> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> </tr> </table> <input type="button" onclick="set()" name="Submit2" value="设置宽度"> </body>
<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>
<tr>
<td width="34%"> </td>
<td width="43%"> </td>
<td width="23%"> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
如果是这样呢?
代码该怎么写?
多谢提示.
但是,能不能直接以象素为单位设置宽度呢?
只能用间接的方法吗?
setWidth("12%")都可以的
return parseInt(iwidth/twidth*100)+'%';
最后还是用的百分比.
能不能用象素呢?
比如这样,得到的百分比就会不太准确:
<table id="table1" width="100%" border="1" cellspacing="0">
<tr>
<td width="25%"> </td>
<td width="25%"> </td>
<td width="25%"> </td>
<td width="25%"> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </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>
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> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
<input type="button" onclick="set()" name="Submit2" value="设置宽度">
</body>