替换dom操作
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <h1>标题1</h1> <h1>标题2</h1> <script type="text/javascript"> var body = document.querySelector('body') var hbiaoti2 = document.querySelectorAll('h1')[1] var newDiv = document.createElement('p') newDiv.innerHTML = '这是一个段落' //这是替换 // body.replaceChild(newDiv,hbiaot //插入到某个元素的前面 body.insertBefore(newDiv,hbiaoti2) //插入到某个元素的后面,原生JS里没有insertAfter body.insertBefore(newDiv,hbiaoti2.nextElementSibling) </script> </body> </html>