替换子节点replaceChild()

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>替换子节点replaceChild()</title>
    <!-- 
        node.replaceChild(a,b)将父节点node的子节点b替换为子节点a
     -->
</head>
<body>
    <div>我要被替换了</div>
    <script>
        var div=document.getElementsByTagName("div")[0];//获取子节点
        var newdiv=document.createElement("div");//创建新的子节点
        newdiv.innerHTML="我是新的,我替换了老的div";//为新的子节点添加内容
        document.body.replaceChild(newdiv,div);//替换
    </script>
</body>
</html>
原文地址:https://www.cnblogs.com/vinson-blog/p/12112755.html