请问怎样得到<td>的宽度?
<script>
function she(tablename){
var td = document.getElementById(tablename).rows[0].cells[1];
alert(td.width); //为什么得不到这个系统自己生成的值
}
</script>
<table id="showTable">
<tr>
<td>序号</td>
<td>姓名</td>
<td>性别</td>
<td>级别</td>
</tr>
</table>
<input type="button" value="onclick" onclick="she('showTable')">
谢谢了
问题点数:100、回复次数:17Top
1 楼adandelion(水源是CSDN最黑的地方,但这个最黑是CSDN一手制造的!)回复于 2005-09-07 21:44:07 得分 10
<script>
function she(tablename){
var td = document.getElementById("td1");
alert(td.width); //为什么得不到这个系统自己生成的值
}
</script>
<table id="showTable">
<tr>
<td id ='td1' width='50'>序号</td>
<td>姓名</td>
<td>性别</td>
<td>级别</td>
</tr>
</table>
<input type="button" value="onclick" onclick="she('showTable')">Top
2 楼aiiiq(外星人木有小JJ)回复于 2005-09-07 21:58:09 得分 10
<table id="showTable">
<tr>
<td onmouseover='alert(offsetWidth)'>序号</td>
<td>姓名</td>
<td>性别</td>
<td>级别</td>
</tr>
</table>
<input type="button" value="onclick" onclick="she('showTable')">
<script>
function she(tablename){
var obj = document.getElementById('showTable').rows[0].cells[1];
alert(obj.offsetWidth); //为什么得不到这个系统自己生成的值
}
</script>
Top
3 楼aiiiq(外星人木有小JJ)回复于 2005-09-07 22:03:33 得分 0
offsetWidthTop
4 楼vivianfdlpw()回复于 2005-09-07 22:04:49 得分 5
alert(td.width);
================>
alert(td.offsetWidth);Top
5 楼andyxin1224(小新)回复于 2005-09-07 22:18:13 得分 5
<table>
<tr>
<td width="value">序号</td>
<td width="value">姓名</td>
<td width="value">性别</td>
<td width="value">级别</td>
</tr>
</table>
这个虽然麻烦,但是实在Top
6 楼qiqunet(瑞旗·广东)回复于 2005-09-08 00:02:01 得分 30
用“clientWidth”:
<script>
function she(tablename){
var td=document.getElementById(tablename).rows[0].cells[1];
alert(td.clientWidth); //为什么得不到这个系统自己生成的值
}
</script>
<table id="showTable">
<tr>
<td>序号</td>
<td>姓名</td>
<td>性别</td>
<td>级别</td>
</tr>
</table>
<input type="button" value="onclick" onclick="she('showTable')">Top
7 楼jbas(jbas)回复于 2005-09-08 13:47:27 得分 0
谢谢各位,我想实现的功能是我动态新增一行后,表格内的text宽度为新增的表格的宽度:
但现在这样不行的.
<html>
<SCRIPT LANGUAGE="JavaScript">
<!--
var rowCount=1;
function addRow(tablename){
tr=document.all.item(tablename).insertRow();
cell=tr.insertCell(); // 编号
cell.innerHTML=rowCount;
cell=tr.insertCell();
rowStr="<input type='text' size="+cell.clientWidth+">";
cell.innerHTML=rowStr;
rowCount=rowCount+1;
}
//-->
</SCRIPT>
<body>
<form name="fm">
<input type="button" value="onclick" onclick="addRow('showTable')">
</form>
<table width="100%" border=1 align="center" cellSpacing=0 borderColorLight=#527DB5 BorderColorDark=#ffffff ID="showTable">
<tr>
<td nowrap>序号</td>
<td nowrap>值</td>
</tr>
</table>
</body>
</html>
Top
8 楼qiqunet(瑞旗·广东)回复于 2005-09-08 19:20:23 得分 0
rowStr="<input type='text' style='width:"+cell.clientWidth+"'>";
Top
9 楼jbas(jbas)回复于 2005-09-08 21:05:24 得分 0
谢谢了,可是还是不行
<html>
<SCRIPT LANGUAGE="JavaScript">
<!--
var rowCount=1;
function addRow(tablename){
tr=document.all.item(tablename).insertRow();
cell=tr.insertCell();
cell.innerHTML=rowCount;
cell=tr.insertCell();
rowStr="<input type='text' style='width:"+cell.clientWidth+"'>";
cell.innerHTML=rowStr;
rowCount=rowCount+1;
}
//-->
</SCRIPT>
<body>
<form name="fm">
<input type="button" value="onclick" onclick="addRow('showTable')">
</form>
<table width="100%" border=1 align="center" cellSpacing=0 borderColorLight=#527DB5 BorderColorDark=#ffffff ID="showTable">
<tr>
<td nowrap>id</td>
<td nowrap>name</td>
</tr>
</table>
</body>
</html>
Top
10 楼qiqunet(瑞旗·广东)回复于 2005-09-08 21:33:10 得分 0
<html>
<SCRIPT LANGUAGE="JavaScript">
<!--
var rowCount=1;
function addRow(tablename){
tr=document.all.item(tablename).insertRow();
cell=tr.insertCell();
cell.innerHTML=rowCount;
cell.style.width=cell.offsetWidth;
cell=tr.insertCell();
rowStr="<input type='text' style='width:"+(cell.clientWidth-2)+"'>";
cell.innerHTML=rowStr;
rowCount=rowCount+1;
}
//-->
</SCRIPT>
<body>
<form name="fm">
<input type="button" value="onclick" onclick="addRow('showTable')">
</form>
<table width="100%" border=1 align="center" cellSpacing=0 borderColorLight=#527DB5 BorderColorDark=#ffffff ID="showTable">
<tr>
<td nowrap>id</td>
<td nowrap>name</td>
</tr>
</table>
</body>
</html>
Top
11 楼jbas(jbas)回复于 2005-09-09 13:42:18 得分 0
谢谢
qiqunet(瑞旗·广东)
大侠,两个是对的,可是当我三个的时候,去又不对了,谢谢了,再帮一次忙.
<html>
<SCRIPT LANGUAGE="JavaScript">
<!--
var rowCount=1;
function addRow(tablename){
tr=document.all.item(tablename).insertRow();
cell=tr.insertCell();
cell.innerHTML=rowCount;
cell.style.width=cell.offsetWidth;
cell=tr.insertCell();
rowStr="<input type='text' style='width:"+(cell.clientWidth-2)+"'>";
cell.innerHTML=rowStr;
cell.style.width=cell.offsetWidth;
cell=tr.insertCell();
rowStr="<input type='text' style='width:"+(cell.clientWidth-2)+"'>";
cell.innerHTML=rowStr;
rowCount=rowCount+1;
}
//-->
</SCRIPT>
<body>
<form name="fm">
<input type="button" value="onclick" onclick="addRow('showTable')">
</form>
<table width="100%" border=1 align="center" cellSpacing=0 borderColorLight=#527DB5 BorderColorDark=#ffffff ID="showTable">
<tr>
<td nowrap>id</td>
<td nowrap>name</td>
<td nowrap>value</td>
</tr>
</table>
</body>
</html>
Top
12 楼chengys()回复于 2005-09-09 15:17:25 得分 0
niu niuTop
13 楼ph580(.Net,我喜欢!www.bjcan.com/hengxing)回复于 2005-09-09 15:26:51 得分 0
UPTop
14 楼jbas(jbas)回复于 2005-09-12 17:17:01 得分 0
大侠再帮一下呀Top
15 楼jbas(jbas)回复于 2005-12-15 19:05:19 得分 0
upTop
16 楼cvpc(一回)回复于 2005-12-16 08:27:39 得分 40
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<SCRIPT LANGUAGE="JavaScript">
<!--
var rowCount=1;
function addRow(tablename){
var aa = document.getElementById("showTable");
var w1 = aa.rows[0].cells[0].clientWidth;
var w2 = aa.rows[0].cells[1].clientWidth;
var w3 = aa.rows[0].cells[2].clientWidth;
tr=document.all.item(tablename).insertRow();
cell=tr.insertCell();
cell.innerHTML=rowCount;
cell.style.width=w1;
cell=tr.insertCell();
rowStr="<input type='text' style='width:99%'>";
cell.innerHTML=rowStr;
cell.style.width=w2;
cell=tr.insertCell();
rowStr="<input type='text' style='width:99%'>";
cell.innerHTML=rowStr;
cell.style.width=w3;
rowCount=rowCount+1;
}
//-->
</SCRIPT>
</head>
<body>
<form name="fm">
<input type="button" value="onclick" onClick="addRow('showTable')">
</form>
<table width="100%" border=1 align="center" cellSpacing=0 borderColorLight=#527DB5 BorderColorDark=#ffffff ID="showTable">
<tr>
<td nowrap>id</td>
<td nowrap>name</td>
<td nowrap>value</td>
</tr>
</table>
</body>
</html>
Top
17 楼jbas(jbas)回复于 2005-12-26 18:56:59 得分 0
谢谢 cvpc(一回)及各位,
cvpc(一回)的可以了,非常感谢!
Top




