如何在内容可编辑div中放置/粘贴光标存在的内容
我是React的新手。我有一个内容可编辑的div。我想要的是按下按钮,单击按钮 - > “嘿”字将添加。假设我的内容长度是100,我单击了最初的5个字符并给出空间并单击我的粘贴按钮。因此,“嘿”内容将在该位置附加。
我的代码:
renderValue = () => {
var textNode = document.createTextNode("HEY");
let p1 = document.getElementById('work').focus();
p1.appendChild(textNode)
}
render(){
<button onClick={() => this.renderValue()}>Paste</button>
<div id="work" onClick={(e)=>e.stopPropagation()}
contentEditable
onInput={this.emitChange}
dangerouslySetInnerHTML={{__html: this.props.html}}
>
</div>
}
我试图附加文本,但没有运气。
安妮的帮助非常感谢。提前致谢。
I am a newbie on react. I have a content editable div. What I want is Make a button, on button click -> "HEY" word will be added. Let's say my content length is 100, and I have clicked on initial 5 character and giving the space and click my paste button. So the "HEY" content will be append in that position.
My code:
renderValue = () => {
var textNode = document.createTextNode("HEY");
let p1 = document.getElementById('work').focus();
p1.appendChild(textNode)
}
render(){
<button onClick={() => this.renderValue()}>Paste</button>
<div id="work" onClick={(e)=>e.stopPropagation()}
contentEditable
onInput={this.emitChange}
dangerouslySetInnerHTML={{__html: this.props.html}}
>
</div>
}
I have tried to append the text but not getting luck.
Anny help is really appreciated. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
document.execcommand()
带有insertText
命令。喜欢You can use
document.execCommand()
withinsertText
command. Like