一个简单的解决方法是不序列化并发送entire选择对象,而是将起点和终点存储为 XPath(及其偏移量)。像这样的事情会做:
function makeXPath (node, currentPath) {
/* this should suffice in HTML documents for selectable nodes, XML with namespaces needs more code */
currentPath = currentPath || '';
switch (node.nodeType) {
case 3:
case 4:
return makeXPath(node.parentNode, 'text()[' + (document.evaluate('preceding-sibling::text()', node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength + 1) + ']');
case 1:
return makeXPath(node.parentNode, node.nodeName + '[' + (document.evaluate('preceding-sibling::' + node.nodeName, node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength + 1) + ']' + (currentPath ? '/' + currentPath : ''));
case 9:
return '/' + currentPath;
default:
return '';
}
}
function restoreSelection () {
var selection = window.getSelection();
selection.removeAllRanges();
var range = document.createRange();
range.setStart(document.evaluate(selectionDetails[0], document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue, Number(selectionDetails[1]));
range.setEnd(document.evaluate(selectionDetails[2], document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue, Number(selectionDetails[3]));
selection.addRange(range);
}
}
function getSelection() {
var selection = window.getSelection();
var range = selection.getRangeAt(0);
var selectObj = {
'startXPath': makeXPath(range.startContainer),
'startOffset': range.startOffset,
'endXPath': makeXPath(range.endContainer),
'endOffset': range.endOffset
}
return selectObj
}
尚未彻底测试,但总体思路就在这里。
资料来源: and That http://home.arcor.de/martin.honnen/javascript/storingSelection1.html