是否可以在文本块中获取标签位置。例如,我有一个巨大的 p 标签,里面是一堆文本。用户将拥有一个工具,该工具会动态地将一堆 span 标签插入到 p 标签中。在某一时刻,用户将完成,我想保存他们所做的事情。由于限制,我无法仅保存 p 标签的全部内容,而是必须获取每个单独的跨度。
初始文本
<p>Sam wanted a dog.
"If you're a good boy," said his father.
"When you can take care of it yourself" said his mother.
Sam cleaned up his room. He ate carrots and broccoli. He stopped making monster noises
at night to scare Molly, his older sister. He hung up his cap after baseball practice.
</p>
用户交互后
<p>Sam wanted a dog.
"If you're <span>a good boy,"</span> said his father.
"When you can take care of it yourself" said his mother.
Sam cleaned up his <span>room. He ate</span> carrots and broccoli. He stopped making monster noises
at night to scare Molly, his older sister. He hung up his cap after baseball practice.
</p>
我想我正在寻找的是跨度开始和结束的范围。到目前为止,我所能做的只是循环浏览内容,但我一直在寻找从那里去的地方。我需要保存的原因是因为用户希望以他们离开的方式返回他们的内容。因此,解决方案将需要考虑将 span 标签放回原来的位置。
我将如何开始的示例 JS
$("p").each(function (index) {
$(this).find("span").each(function () {
console.log(this);
});
});
我的真实环境更复杂,但我已将其简化为基础,以缩小解决方案的范围。非常感谢任何帮助/建议。