<!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>拆分文本节点splitText()</title> <!-- node.splitText(n);合并文本节点node从索引n开始拆分,包括索引n 原本的文本节点node会被修改 --> </head> <body> <div id="a"></div> <script> var div=document.getElementById("a"); var text1=document.createTextNode("我是谁?"); div.appendChild(text1); var text2=div.firstChild.splitText(2);//从索引2开始拆分,包含2 console.log(text2);//谁? console.log(text1);//我是 </script> </body> </html>
原文地址:https://www.cnblogs.com/vinson-blog/p/12112770.html
时间: 2024-10-02 04:45:07