gpt4 book ai didi

javascript - 保存选定的文本范围以备后用 不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 05:01:06 25 4
gpt4 key购买 nike

我正在使用 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/

为什么当我备份范围并想稍后恢复它时,它不起作用?我是否需要以不同的方式备份范围?

最佳答案

您可以通过存储 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');
});

关于javascript - 保存选定的文本范围以备后用 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20978338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com