cloneNode和replaceChild

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>

  

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