admin管理员组文章数量:1516870
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>
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>
Share
asked Apr 8, 2013 at 22:03
fbynitefbynite
2,6611 gold badge22 silver badges25 bronze badges
2
- 1 Using IE10 and it seems to work in IE8, IE9, and IE10 mode. It does not work in IE7 mode though. – Michael Todd Commented Apr 8, 2013 at 22:09
- @MichaelTodd, The IE8 Document Mode was set to Quirks. Switching to IE8 Standards did the trick. Thanks. – fbynite Commented Apr 8, 2013 at 22:27
3 Answers
Reset to default 3You can't use only createElement() function to create table, tr, td to create whole table element.
Instead you have to use insertRow(), insertCell() function of table Object
For Reference check this:
http://viralpatel/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 tbody object and append that to the table object and tr to that tbody object
tb = document.createElement("tbody")
var tbody = document.createElement('tbody');
table.appendChild(tbody);
var table_row = document.createElement('tr');
tbody.appendChild(table_row)//
just add <!DOCTYPE html> in markup.
In IE7 and above default rendering without DOCTYPE is Quirks mode (IE5).
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);
本文标签: javascriptdocumentcreateElement on tableTRtd tags fails IE8Stack Overflow
版权声明:本文标题:javascript - document.createElement on table,tr,td tags fails IE8 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.betaflare.com/web/1743688872a2522360.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论