Save Selected Text Range for Use Later Not working(保存选定的文本范围以供以后使用 不工作)
问题描述
我正在使用 contenteditable
并突出显示一些文本.然后我想备份那个文本范围,然后给那个范围(文本)一个不同的颜色.如果我签入我的 zss_editor.restorerange
方法,我确实会返回一个有效的 selection
对象,因此我之前保存该范围的方式一定是不正确的.
I am using contenteditable
and highlighting some text. I want to then backup that text range, and at a later time give that range(text) a different color. If I check in my zss_editor.restorerange
method I do get back a valid selection
object, so it must be something incorrect in how I am previously saving that range.
var zss_editor = {};
// The current selection
zss_editor.currentSelection;
zss_editor.backuprange = function(){
var selection = window.getSelection();
zss_editor.currentSelection = selection.getRangeAt(0);
zss_editor.currentSelection.setEnd(zss_editor.currentSelection.startContainer, zss_editor.currentSelection.startOffset);
}
zss_editor.restorerange = function(){
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(zss_editor.currentSelection);
console.log(zss_editor.currentSelection);
}
zss_editor.setTextColor = function(color) {
zss_editor.restorerange();
document.execCommand("styleWithCSS", null, true);
document.execCommand('foreColor', false, color);
document.execCommand("styleWithCSS", null, false);
}
zss_editor.setBackgroundColor = function(color) {
zss_editor.restorerange();
document.execCommand("styleWithCSS", null, true);
document.execCommand('hiliteColor', false, color);
document.execCommand("styleWithCSS", null, false);
}
关于 JS Fiddle 的工作示例:http://jsfiddle.net/zedsaid/gC3jq/11/
Working example on JS Fiddle: http://jsfiddle.net/zedsaid/gC3jq/11/
为什么,当我备份范围并想在以后恢复它时,它不起作用?我需要以其他方式备份范围吗?
推荐答案
可以通过存储startContainer &startOffset 以及 endContainer &结束偏移.要恢复,您只需创建一个新的范围对象并设置该范围对象的开始和结束,然后将其添加到选择中
You can backup the range by storing the startContainer & startOffset as well as the endContainer & endOffset. To restore, you just create a new range object and set the start and end of that range object then add it to the selection
var zss_editor = {};
// The current selection
zss_editor.currentSelection;
zss_editor.backuprange = function(){
var selection = window.getSelection();
var range = selection.getRangeAt(0);
zss_editor.currentSelection = {"startContainer": range.startContainer, "startOffset":range.startOffset,"endContainer":range.endContainer, "endOffset":range.endOffset};
}
zss_editor.restorerange = function(){
var selection = window.getSelection();
selection.removeAllRanges();
var range = document.createRange();
range.setStart(zss_editor.currentSelection.startContainer, zss_editor.currentSelection.startOffset);
range.setEnd(zss_editor.currentSelection.endContainer, zss_editor.currentSelection.endOffset);
selection.addRange(range);
console.log(range);
}
zss_editor.setTextColor = function(color) {
zss_editor.restorerange();
document.execCommand("styleWithCSS", null, true);
document.execCommand('foreColor', false, color);
document.execCommand("styleWithCSS", null, false);
}
zss_editor.setBackgroundColor = function(color) {
zss_editor.restorerange();
document.execCommand("styleWithCSS", null, true);
document.execCommand('hiliteColor', false, color);
document.execCommand("styleWithCSS", null, false);
}
$('#backup').click(function() {
zss_editor.backuprange();
});
$('#color1').click(function() {
zss_editor.setTextColor('#007AFF');
});
$('#color2').click(function() {
zss_editor.setBackgroundColor('#007AFF');
});
这篇关于保存选定的文本范围以供以后使用 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:保存选定的文本范围以供以后使用 不工作


- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01