日期:2014-05-16  浏览次数:20350 次

JS增加表格行出问题了。。。
我用JS增加表格行,怎么加不上去呢?一加上马上就消失了。。。

<!DOCTYPE html>
<html>
  <head>
    <title>test.html</title>
<script type="text/javascript">
function addRow() {
var tbl = document.getElementById("tbl");

var newRow = tbl.insertRow(1);

var newCell1 = newRow.insertCell();

newCell1.innerHTML = "newCell";
alert(1);
}
</script>
  </head>
  
  <body>
    <table id="tbl" border="1">
     <tbody id="tbl">
     <tr>
     <td>123</td>
     <td>456</td>
     </tr>
     </tbody>
    </table>
    <a href="" onclick="addRow()">add</a>
  </body>
</html>

------解决方案--------------------
<!DOCTYPE html>
<html>
  <head>
    <title>test.html</title>
<script type="text/javascript">
function addRow() {
var tbl = document.getElementById("tbl");

var newRow = tbl.insertRow(1);

var newCell1 = newRow.insertCell();

newCell1.innerHTML = "newCell";
alert(1);
}
</script>
  </head>
  
  <body>
    <table id="tbl" border="1">
     <tbody id="tbl">
     <tr>
      <td>123</td>
      <td>456</td>
     </tr>
     </tbody>
    </table>
    <a href="javascript:void(0)" onclick="addRow()">add</a>
  </body>
</html>
a标签href添加红色部分  你页面跳转了