<!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>合并文本节点normalize()</title> <!-- node.normalize();合并节点node内相邻的文本节点并删除空的文本节点 --> </head> <body> <div id="a"></div> <script> var div=document.getElementById("a"); var text1=document.createTextNode("我是"); var text2=document.createTextNode("谁?"); var text3=document.createTextNode("我就是我"); div.appendChild(text1); div.appendChild(text2); div.appendChild(text3); console.log(div.childNodes.length);//3,合并前子节点长度 div.normalize();//将div内的所有节点合并 console.log(div.childNodes.length);//1,合并后子节点长度 </script> </body> </html>
原文地址:https://www.cnblogs.com/vinson-blog/p/12112765.html
时间: 2024-10-14 05:37:34