Javascript动态添加多行

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=GBK">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title></title>
  </head>
  <body>
  <script type="text/javascript">
 //删除表格数据
   function delTable(){
  var theTable = document.getElementById("tblZhouqi");
  while(theTable.hasChildNodes()){
   theTable.removeChild(theTable.lastChild);
  }
 }
 //添加数据 
   function addToTable(){
  var lenZhouqi = document.getElementById("txtZhouqi").value;
  var theTable = document.getElementById("tblZhouqi");
  //清理数据
  delTable();
  for(var i=1,len=lenZhouqi;i<=len;i++){
   var theRow = theTable.insertRow();
   //第一列
   var theCell = theRow.insertCell();
   theCell.style.backgroundColor = "#cccccc";
   theCell.innerText = "第" + i + "号";
   theCell.value = i;
   //第二列  这里可以if(i == 1){theCell.innerText =""}
   //else if(i == 2){theCell.innerText =""}  或者想其他办法进行赋值,比如调用一个函数 getValue(i) ,函数里根据i返回值
   theCell = theRow.insertCell();
   theCell.style.backgroundColor = "#cccccc";
   theCell.innerText = "小明";
   theCell.value = "小明";
   //第三列
   theCell = theRow.insertCell();
   theCell.style.backgroundColor = "#cccccc";
   theCell.innerText = "20";
   theCell.value = "20";
  // alert("22222");
  }
 }
  </script>
 
  <p>行数:
    <input id="txtZhouqi" name="txtZhouqi" type="text">
 <input id="btnAdd" name="btnAdd" value="增加" type="button" onClick="addToTable()">
  </p>
  <br />
  <table id="tblZhouqiHeader" name="tblZhouqi" width="85%" border="0" cellpadding="0" cellspacing="2">
    <tr>
      <td bgcolor="#aaaaaa">序号</td>
   <td bgcolor="#aaaaaa">名字</td>
   <td bgcolor="#aaaaaa">年龄</td>
    </tr>
  </table>
  <table id="tblZhouqi" name="tblZhouqi" width="85%" border="0" cellpadding="0" cellspacing="2" style="padding-top:0px;margin-top:0px;">
  </table>
 
  </body>
</html>

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值