javascript html dom的节点问题
如下:<bodyname="ddd"><divid="t"><span>aaa</span><span>bbb</span><span>ccc</span></div><...
如下:
<body name="ddd">
<div id = "t">
<span>aaa</span>
<span>bbb</span>
<span>ccc</span>
</div>
</body>
<script>
var newNode = document.createElement("span");
newNode.innerHTML = "eee";
var oldNode = document.getElementById("t").lastChild;
document.getElementById("t").replaceChild(newNode,oldNode);
</script>
为什么最后一项是ccceee,而不是eee,没有替换啊? 展开
<body name="ddd">
<div id = "t">
<span>aaa</span>
<span>bbb</span>
<span>ccc</span>
</div>
</body>
<script>
var newNode = document.createElement("span");
newNode.innerHTML = "eee";
var oldNode = document.getElementById("t").lastChild;
document.getElementById("t").replaceChild(newNode,oldNode);
</script>
为什么最后一项是ccceee,而不是eee,没有替换啊? 展开
2个回答
展开全部
<body name="ddd">
<div id = "t">
<span>aaa</span>
<span>bbb</span>
<span>ccc</span>
</div>
</body>
<script type="text/javascript">
var newNode = document.createElement("b");
var oldNode = document.getElementById("t").children(2);
alert(oldNode.innerHTML);
document.getElementById("t").replaceChild(newNode,oldNode);
newNode.innerHTML = "eee";
</script>
把lastchild换了
<div id = "t">
<span>aaa</span>
<span>bbb</span>
<span>ccc</span>
</div>
</body>
<script type="text/javascript">
var newNode = document.createElement("b");
var oldNode = document.getElementById("t").children(2);
alert(oldNode.innerHTML);
document.getElementById("t").replaceChild(newNode,oldNode);
newNode.innerHTML = "eee";
</script>
把lastchild换了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询