按钮混淆了

时间:2014-01-26 03:24:56

标签: javascript html dynamic

我的问题是,当我单击表格中的添加行按钮时,它仅在第一个单元格中而不是在新单元格中添加新行。有没有办法纠正这个?

<html>
<body>
<input name="button2" type="button" onClick="addRow('dataTable')" value="Add Row" />
<table id="dataTable" width="150px" border="1">
        <tr>
  <td height="84">
      1
  </td>
  <td>
      <input type="text" />
  </td>
    <td> <input name="button" type="button" onClick="addRow('dataTable1')" value="Add Row" /> 
      <table id="dataTable1" width="150px" border="1">
        <tr> 
          <td height="27"> 1 </td>
          <td> <input name="text" type="text" /> </td>
          <td> <input name="text" type="text" /> </td>
          <td> <input name="button" type="button" onClick="addRow('dataTable1')" value="Add Row" />
        </tr>
      </table>
      </td>
        </tr>
    </table>
</body>
</html>
<script type="text/javascript">
var contents = document.getElementById('dataTable').outerHTML;
var contents = document.getElementById('dataTable1').outerHTML;


function addRow(tableID) {
  var table = document.getElementById(tableID);
  var rowCount = table.rows.length;
  var row = table.insertRow(rowCount);
  var firstCell = row.insertCell(0);
  firstCell.setAttribute('colspan', 3);
  var newCont = contents.replace(' 1 ', rowCount + 1);
  firstCell.innerHTML = newCont;
}

</script>

0 个答案:

没有答案
相关问题