測試程式碼:
<script> <BR>var oTable=document.getElementById("test"); <BR>oTable.innerHTML="<tr><td>innerHTML "; <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 , 和
objects.
複製程式碼
程式碼如下:
var oTable=document.getElementById("test");
//oTable.innerHTML="
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;
} else {
table.innerHTML=html;
} 這裡只是對table做了處理,對其他不支援的元素可以用類似的方案。 另外,IE10中table已經支援innerHTML了。 作者:Artwl