我想在文档中添加html标签最初是这样的做的,但是太麻烦。<body></body><script>function XX(){ var t1 = document.createElement("table"); var t2 = document.createElement("tr");
var t3 = document.createElement("td"); var t4 = document.createTextNode("11111"); var t5 = document.createElement("td");
var t6 = document.createTextNode("22222"); document.body.appendChild(t1);
t1.appendChild(t2);
t2.appendChild(t3);
t3.appendChild(t4);
t2.appendChild(t5);
t5.appendChild(t6);
t1.setAttribute("border","1");
t1.setAttribute("width","100%");
}
XX();</script>后来有这样做,可是不兼容ie。<script>window.onload=function x(){
document.body.innerHTML="<table><tr><td>1sssss1</td><td>22sss</td></tr><tr><td>1sss1</td><td>22sss</td></tr></table>"
}</script>有什么好办法吗?谢谢
1 回答
白猪掌柜的
TA贡献1893条经验 获得超10个赞
试试这个。
<table id="table"></table>
var str = "<tr><td>1sssss1</td><td>22sss</td></tr><tr><td>1sss1</td><td>22sss</td></tr>"; setTableInnerHTML(document.getElementById('table'), str); function setTableInnerHTML(table, html) { if (navigator && navigator.userAgent.match(/msie/i)) { var temp = table.ownerDocument.createElement('div'); temp.innerHTML = '<table><tbody>' + html + '</tbody></table>'; 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; } }
添加回答
举报
0/150
提交
取消