<html>
<body>
<table id="mytable">
<tr>
<th >adf</th>
<th >123</th>
<th >342</th>
<th >4545</th>
</tr>
</table>
<script>
var fragment = document.createDocumentFragment();
var tableElement = document.getElementById("mytable");
alert(tableElement .innerHTML); //输出值为<TBODY><TR>.....</TR></TBODY>
alert(tableElement .outerHTML);//输出值为<TABLE id=mytable><TBODY><TR>...</TR></TBODY></TABLE>
for (var i=0; i<10; i++) {
var tr = document.createElement("tr");
for (var j=0; j<4; j++) {
var td = document.createElement("td");
td.innerHTML = "111";
tr.appendChild(td);
fragment.appendChild(tr);
}
}
tableElement .appendChild(fragment);
//如果注释掉这句,在ie8下不会增加tr,在firefox下表现正常
//判断是否是ie浏览器,如果是的话用tableElement .outerHTML = tableElement .outerHTML保证append标签正确使用
if(document.all) {
tableElement .outerHTML = tableElement .outerHTML; //outHTML是ie下特有的属性,firefox下没有
}
</script>
</body>
</html>