简体   繁体   English

在插入符号位置插入HTML,而不创建对象

[英]insertHTML in the caret position without creating object

I have contenteditable div, and span with text inside. 我有令人满意的div,并且里面有文字。

<div contenteditable="true"><span> Some text.. </span></div>

I need insert in place of caret closing and opening span tags. 我需要插入代替插入符号和打开span标记。 Like: 喜欢:

<div contenteditable="true"><span> Some </span><span> text.. </span></div>

I tried to make it by insertHTML: 我尝试通过insertHTML来实现它:

document.execCommand('insertHTML', false, '</span><span>');

It works in Crome, but Firefox makes span object and insert here (inserts a valid html). 它适用于Crome,但Firefox会在这里插入span对象并插入(插入有效的html)。 It turns out: 事实证明:

<div contenteditable="true"><span> Some <span></span> text.. </span></div>

Does anyone know how to split the span tag inside contenteditable div? 有谁知道如何在contenteditable div中拆分span标签? Thanks in advance 提前致谢

You could just use the innerHTML property to insert any HTML code, here's an example using javascript: 您可以使用innerHTML属性插入任何HTML代码,这是使用javascript的示例:

document.getElementById("myDiv").innerHTML = "<span>Here is the new Content!</span>";

And give your div the id="myDiv" . 并给你的div id="myDiv"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM