javascript动态增删Table

时间:2022-03-30 03:47:44

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> 动态增删表格 </TITLE>
  <SCRIPT LANGUAGE="JavaScript">
  <!--
 var i=1;
 function addTable(content)
 {
  var categoryTable = document.getElementById("MyTable");
  var newRow=categoryTable.insertRow(0);//如果从最后增加,insertRow不需要参数
  var newCell=newRow.insertCell();
  newCell.innerHTML=content+(i++);
 }
 function delTable()
 {
  var categoryTable = document.getElementById("MyTable");
  if(categoryTable.rows.length>0){
   categoryTable.deleteRow(0);//如果删除最后行,deleteRow不需要参数
  }
 }
  //-->
  </SCRIPT>
 </HEAD>

 <BODY>
 <table id="MyTable" border="0" cellpadding="0"></table>
 <input type=Button onclick="addTable('NewRow ')" value="add">
 <input type=Button onclick="delTable()" value="del">
 </BODY>
</HTML>