4

As the title says, I'm having an issue with IE8 (works in FF and IE9). The following code doesn't produce any errors, and if I substitute div,ul,and li; it works. I did some searching and didn't find anything on (table,tr,td) tags not being supported using document.createElement with IE8. Where am I going wrong?

Here is the code:

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <title>My Page Title</title>
  <script>
    function init() {
      var ele = document.getElementById('content');
      var table = document.createElement('table');
      var tr = document.createElement('tr');
      var td = document.createElement('td');
      var txt = document.createTextNode('IE8');

      td.appendChild(txt);
      tr.appendChild(td);
      table.appendChild(tr);
      ele.appendChild(table);
    }
   </script>
  </head>
  <body onload="init();">
    <div id="content"></div>
  </body>
</html>
4

3 回答 3

3

您不能仅使用createElement()函数来创建table, trtd创建整个表格元素。

相反,您必须使用表对象的insertRow()功能insertCell()

供参考检查:

http://viralpatel.net/blogs/dynamically-add-remove-rows-in-html-table-using-javascript/

编辑:

即使我对 IE 问题也有同样的想法,购买我发现实际上要createElement()在 IE7 中工作,您必须创建tbody对象并将其附加到table对象和trtbody对象

tb = document.createElement("tbody")  
var tbody  = document.createElement('tbody'); 
table.appendChild(tbody);
var table_row  = document.createElement('tr'); 
tbody.appendChild(table_row)// 
于 2013-04-08T22:07:33.017 回答
1

尝试这个:

  var ele = document.getElementById('content');
  var table = document.createElement('table');
  ele.appendChild(table);
  var tr = document.createElement('tr');
  table.appendChild(tr);
  var td = document.createElement('td');
  tr.appendChild(td);
  var txt = document.createTextNode('IE8');
  td.appendChild(txt);
于 2014-11-16T13:15:34.720 回答
1

只需添加<!DOCTYPE html>标记。在 IE7 及更高版本中,没有 DOCTYPE 的默认呈现是 Quirks 模式 (IE5)。

于 2013-04-08T22:24:51.943 回答