简体   繁体   English

为什么不能在Internet Explorer中使用JavaScript将行动态添加到HTML表中?

[英]Why can't I dynamically add rows to a HTML table using JavaScript in Internet Explorer?

In Firefox it works, in my Internet Explorer 6 or 7 it doesn't: 在Firefox中可以使用,在Internet Explorer 6或7中不能使用:

<html>
<head>

    <script type="text/javascript">
      function newLine() {
        var tdmod = document.createElement('td');
        tdmod.appendChild(document.createTextNode("dynamic"));

        var tr = document.createElement('tr');
        tr.appendChild(tdmod);

        var tt  = document.getElementById("t1");
        tt.appendChild(tr);
      }     

    </script>
</head>
<body>

    <a href="#" onclick="newLine()">newLine</a>

      <table id="t1" border="1">
        <tr>
          <td>
          static
          </td>  
        </tr>
      </table>

</body>

The user clicks on the link "newLine" and new rows should be added to the table. 用户单击链接“ newLine”,新行应添加到表中。

How to make this work also in IE? 如何在IE中也进行这项工作?

Edit: Thanks to the accepted answer I changed it like this and now it works: 编辑:感谢接受的答案,我这样更改它,现在它可以工作:

  <table border="1">
   <tbody id="t1">
    <tr>
      <td>
      static
      </td>  
    </tr>
   </tbody>
  </table>

(未测试),您可以尝试将该行附加到tbody元素上,该元素通常是自动创建的,也可以是您自己定义的。

Always put 总是放

<tbody> 

in table for IE 在IE的表格中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM