CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Web 开发 >  JavaScript

请问怎样得到<td>的宽度?

楼主jbas(jbas)2005-09-07 20:42:32 在 Web 开发 / JavaScript 提问

<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

相关问题

  • 怎样得到中文字符串的宽度?(空)
  • 怎样才能得到主菜单的宽度值?
  • 请问怎样得到一幅图片高度,宽度?
  • 怎样得到Listview的ListItem的高度,和宽度?
  • 怎样得到英文字符的宽度?
  • 怎样得到一个窗口的标题条宽度和其边框的宽度
  • 怎样得到一个图形文件的高度和宽度呢?
  • 请问:怎样得到一个字符串得宽度,不是长度????????????????????
  • 请问各位,怎样得到一个框架的宽度和高度呀
  • 如何得到宽度???

关键词

得分解答快速导航

  • 帖主:jbas
  • adandelion
  • aiiiq
  • vivianfdlpw
  • andyxin1224
  • qiqunet
  • cvpc

相关链接

  • Web开发类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo