gpt4 book ai didi

javascript - 在文本之间插入一个节点

转载 作者:数据小太阳 更新时间:2023-10-29 04:22:41 25 4
gpt4 key购买 nike

我熟悉在给定引用节点之后或之前插入文本节点。但是,我想知道如何在给定节点的文本之间插入标签。例如,

Before insertion: <p>Lorem dolor</p>
After insertion: <p>Lorem <span>ipsum</span> dolor</p>

span 节点必须在另一个节点内的 N 个字符(我不需要用户的光标选择位置)之后插入。这可能吗?

最佳答案

正确的方法(使用 DOM-Core 接口(interface))是:

var p = document.getElementById('myParagraph');
var text = p.childNodes[0];
var at = 5;

// create new span node with content
var span = document.createElement("span");
span.appendChild(document.createTextNode('ipsum'));

// Split the text node into two and add new span
p.insertBefore(span, text.splitText(at));

关于javascript - 在文本之间插入一个节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/374041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com