admin管理员组

文章数量:1432001

In Firefox it works, in my Internet Explorer 6 or 7 it doesn't:

<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.

How to make this work also in 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>

In Firefox it works, in my Internet Explorer 6 or 7 it doesn't:

<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.

How to make this work also in 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>
Share Improve this question edited Apr 8, 2010 at 21:37 user89021 asked Apr 8, 2010 at 21:20 user89021user89021 15.2k16 gold badges55 silver badges65 bronze badges 3
  • wow, a Javascript code not working in IE? – Samuel Carrijo Commented Apr 8, 2010 at 21:26
  • 1 Yeah; never ever heard of that happening before. Inconceivable! – Donal Fellows Commented Apr 8, 2010 at 21:28
  • I knew the headline would somehow get your attention ;-) – user89021 Commented Apr 8, 2010 at 21:30
Add a ment  | 

2 Answers 2

Reset to default 7

(untested) you might try appending the row to a tbody element, either the one that is usually created automatically or one you define yourself.

Always put

<tbody> 

in table for IE

本文标签: Why can39t I dynamically add rows to a HTML table using JavaScript in Internet ExplorerStack Overflow