替换子节点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>
    
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>

你可能感兴趣的:(替换子节点replaceChild())