js的insertBefore函数插如tr节点兼容性问题?

<html>
<head>
<title>test</title>

</head>
<body>
<div id="div1"><b>aaa</b></div>
<table id="table1">
<tr id="tr1"><td>1</td><td>2</td></tr>
<tr id="tr2"><td>3</td><td>4</td></tr>
</table>
</body>
<script type="text/javascript">
var table1 = document.getElementById("table1");
var tr1 = table1.rows[0];
var htmlSpan = document.createElement("tr");
htmlSpan.innerHTML = "<td>aaa</td><td>bbb</td>";
//htmlSpan.style.height = 25;
tr1.parentNode.insertBefore(htmlSpan,tr1);
</script>
</html>

为什么这段代码在IE6下无法成功。

慕神8447489
浏览 1201回答 2
2回答

慕桂英4014372

htmlSpan.innerHTML = "<td>aaa</td><td>bbb</td>";主要是这在ie中不会生成规范的td,既然create了tr就在create dt吧,如下<script type="text/javascript">var table1 = document.getElementById("table1");var tr1 = table1.rows[0];var htmlSpan = document.createElement("tr");var td1 = document.createElement("td");var td2 = document.createElement("td");td1.innerHTML = "aaa";td2.innerHTML = "bbb";htmlSpan.appendChild(td1);htmlSpan.appendChild(td2);//htmlSpan.style.height = 25;tr1.parentNode.insertBefore(htmlSpan,tr1);</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Unity 3D