contenteditable 选定的文本保存和恢复
我发现这篇文章显示了如何从 contenteditable div 保存和恢复所选文本的 2 个函数。我将下面的 div 设置为 contenteditable 以及另一篇文章中的 2 函数。如何使用这些功能来保存和恢复选定的文本。
<div style="width:300px;padding:10px;" contenteditable="true">test test test test</div>
<script>
function saveSelection() {
if (window.getSelection) {
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
return sel.getRangeAt(0);
}
} else if (document.selection && document.selection.createRange) {
return document.selection.createRange();
}
return null;
}
function restoreSelection(range) {
if (range) {
if (window.getSelection) {
sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (document.selection && range.select) {
range.select();
}
}
}
</script>
I came across this post that shows 2 functions on how to save and restore selected text from a contenteditable div. I have the below div set as contenteditable and the 2 function from the other post. How to i use these functions to save and restore selected text.
<div style="width:300px;padding:10px;" contenteditable="true">test test test test</div>
<script>
function saveSelection() {
if (window.getSelection) {
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
return sel.getRangeAt(0);
}
} else if (document.selection && document.selection.createRange) {
return document.selection.createRange();
}
return null;
}
function restoreSelection(range) {
if (range) {
if (window.getSelection) {
sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (document.selection && range.select) {
range.select();
}
}
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
典型的用途是显示某种小部件或对话框以接受用户的输入(从而可能破坏原始选择)并在隐藏该小部件后恢复选择。实际上使用这些功能非常简单;最大的危险是在选择已被破坏后试图保存它。
这是一个简单的例子。它显示文本输入并使用该输入中的文本覆盖可编辑
中的选择。请注意,此代码在
document.selection
分支中支持 Internet Explorer <= 8,现在可能会在 2022 年删除该支持:A typical use would be displaying some kind of widget or dialog to accept input from the user (thus potentially destroying the original selection) and restoring the selection after that widget has been hidden. Actually using the functions is quite simple; the biggest danger is trying to save the selection after it has already been destroyed.
Here's a simple example. It displays a text input and overwrites the selection in the editable
<div>
with the text from that input. Note that this code has support for Internet Explorer <= 8 in thedocument.selection
branches which could be removed now, in 2022:只是一个建议:
很难使用本机浏览器选择 + contenteditable + 处理所有不同的浏览器方面 + 从头开始保存和恢复选择等。
我会推荐 rangy https://code.google.com/p/rangy/wiki/SelectionSaveRestoreModule
专门为您完成所有艰苦的选择工作而
检查文档,它很容易使用;)
希望对你有帮助
just one recommendation:
it is hard to work with native browser selection + contenteditable + handle all different browser aspects + save and restore selection and etc from scratch..
I would recomend rangy https://code.google.com/p/rangy/wiki/SelectionSaveRestoreModule
that specially done to make all hard work with selection for you
check the docs, it is easy to use ;)
hope it will help you