Сохранить выбранный диапазон текста для последующего использования Не работает
я использую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/
Почему, когда я делаю резервную копию диапазона и хочу восстановить его позднее, он не работает? Нужно ли резервировать диапазон другим способом?