首页 > 其他 > 详细

cloneNode和replaceChild

时间:2018-03-24 18:58:54      阅读:225      评论:0      收藏:0      [点我收藏+]

 

node.cloneNode(deep)

var node=document.getElementById("myList2").lastChild.cloneNode(true);
document.getElementById("myList1").appendChild(node);

var node = document.getElementById("demo");
		console.log(node.firstChild);
		console.log(node.firstElementChild);
		console.log(node.childNodes);

  如果您需要克隆所有后代,请把 deep 参数设置 true,否则设置为 false。

node.replaceChild(newnode,oldnode);

<ul id="myList"><li>Coffee</li><li>Tea</li><li>Milk</li></ul>

<p id="demo">点击按钮来替换列表中的首个项目。</p>

<button onclick="myFunction()">试一下</button>

<script>
function myFunction()
{
var textnode=document.createTextNode("Water");
var item=document.getElementById("myList").childNodes[0];
item.replaceChild(textnode,item.childNodes[0]);
}
</script>

  

cloneNode和replaceChild

原文:https://www.cnblogs.com/yiyi17/p/8640666.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!