相当于内容可编辑的跨度和div的selectionStart、selectionEnd和focus()
我正在尝试设置一个 contenteditable
范围,以便它在 onkeyup
事件上用格式化文本重新填充该范围。
然而,当我尝试这样做时,光标消失了,我无法再打字了。这是因为格式化程序不会修改跨度中已有的文本,而是将其全部擦除,然后将格式化的文本写入其位置。
重要的是(我认为是)要注意该元素似乎没有失去焦点。另外,因为我希望这个项目在发布之前保持相当“秘密”,所以我宁愿现在不泄露源代码。
I am trying to set a contenteditable
span so that it will on the onkeyup
event refill the span with formatted text.
However, when I try this the cursor disappears and I can't type anymore. This is because the formatting program, instead of modifying the text already in the span, erases it all and then writes the formatted text in its place.
It is important (I think it is) to note that the element does not appear to be losing focus. Also, because I would like for this project to remain fairly "secret" until it's release, I would rather not give away the source code right now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是由于您使用innerHTML 设置格式化文本。
而是使用 childNodes 集合来访问内容并用格式化的 html 元素替换 textNodes。这样可以避免设置innerHTML并避免失去焦点。
This is probably due to you using the innerHTML to set the formatted text.
Instead use childNodes collection to access the content and replace textNodes with formatted html element. This avoids setting innerHTML and avoids loosing focus.