【问题标题】:Save Selected Text Range for Use Later Not working保存选定的文本范围以供以后使用 不工作
【发布时间】:2014-01-07 17:45:25
【问题描述】:

我正在使用contenteditable 并突出显示一些文本。然后我想备份那个文本范围,然后给那个范围(文本)一个不同的颜色。如果我签入我的zss_editor.restorerange 方法,我确实会返回一个有效的selection 对象,所以我之前保存该范围的方式一定是不正确的。

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/

为什么,当我备份范围并想在以后恢复它时,它不起作用?我需要以其他方式备份范围吗?

【问题讨论】:

  • 您没有创建范围的“备份”,您需要 cloneRange() 来复制范围。
  • @Teemu 你能举个例子吗?
  • Here... 虽然在改变背景时有些奇怪,但在模糊和重新聚焦窗口后它会改变。

标签: javascript range contenteditable execcommand


【解决方案1】:

您可以通过存储 startContainer 和 startOffset 以及 endContainer 和 endOffset 来备份范围。要恢复,您只需创建一个新的范围对象并设置该范围对象的开始和结束,然后将其添加到选择中

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');
});

【讨论】:

  • 这给了我错误:TypeError: '[object RangeConstructor]' is not a constructor (evaluating 'new Range()')
  • 你用的是什么浏览器?在 JSFiddle 中测试时没有收到错误
  • 使用 Safari 进行测试。
  • 我对代码进行了更改以使用 document.createRange() 而不是使用 new Range()。这应该有望解决问题。
  • 不,它在 Firefox 或 Safari 中不起作用。它赋予它我想要的颜色,但我很想再次赋予该范围焦点并选择它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多