javascript 表、tr、td 标签上的 document.createElement 失败 IE8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15889840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:28:19  来源:igfitidea点击:

document.createElement on table,tr,td tags fails IE8

javascriptcreateelement

提问by fbynite

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.createElementwith IE8. Where am I going wrong?

正如标题所说,我在使用 IE8 时遇到了问题(适用于 FF 和 IE9)。下面的代码不会产生任何错误,如果我替换 div、ul 和 li;有用。我做了一些搜索,但没有找到任何不支持使用document.createElementIE8 的(table,tr,td) 标签。我哪里错了?

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>

回答by prabeen giri

You can't use only createElement()function to create table,tr, tdto create whole table element.

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

Instead you have to use insertRow(), insertCell()function of table Object

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

For Reference check this:

对于参考检查这个:

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

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

Edit:

编辑:

Even I was thinking in the same way for IE issues , Buy I found that actually for createElement()to work in IE7 you have to create tbodyobject and append that to the tableobject and trto that tbodyobject

即使我以同样的方式思考 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)// 

回答by raj-nt

just add <!DOCTYPE html>in markup. In IE7 and above default rendering without DOCTYPE is Quirks mode (IE5).

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

回答by saravanan

try this:

试试这个:

  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);