テストコード:
<script> <br>var oTable=document.getElementById("test"); <br>oTable.innerHTML="<tr><td> innerHTML</ td></tr>"; <br></script>
上述代码在IE6-9中无效,直接报错:
IE9:Invalid target element for this operation.
IE6-8:Unknown runtime error
查找IE的文档(
http://msdn.microsoft.com/en-us/library/ms533897(VS.85).aspx)后发现有这么一段:
The innerHTML property is read-only on the col, colGroup, frameSet, html, head, style, table, tBody, tFoot, tHead, title, and tr objects.
So we can only use other solutions. My solution:
var oTable=document.getElementById("test");
//oTable.innerHTML="
< ;td>innerHTML
";
setTableInnerHTML(oTable,"
innerHTML |
");
function setTableInnerHTML(table, html) {
if(navigator && navigator.userAgent.match(/msie/i)){
var temp = table.ownerDocument.createElement('div');
temp. innerHTML = '
';
if(table.tBodies.length == 0){
var tbody=document.createElement ("tbody");
table.appendChild(tbody);
}
table.replaceChild(temp.firstChild.firstChild, table.tBodies[0]);
} else {
table.innerHTML=html;
}
}
Here we only deal with the table. A similar solution can be used for other unsupported elements.
In addition, tables in IE10 already support innerHTML.
Author: Artwl