[英]How can I save and restore Selection range in javascript?
I want to save (serialize) the Selection
range in order to reuse (deserialize) it in the next user session in my UIWebView
of iOS app .我想保存(序列化)
Selection
范围,以便在我的UIWebView
应用程序的 UIWebView 中的下一个用户 session 中重用(反序列化)它。
Userpath :用户路径:
My idea is firstly get the range by calling window.getSelection().getRangeAt(0)
and then save its startContainer
, endContainer
properties.我的想法是首先通过调用
window.getSelection().getRangeAt(0)
获取范围,然后保存其startContainer
、 endContainer
属性。 Check the following demo snippet:检查以下演示片段:
function saveSelectedRange() { var highlightRange = window.getSelection().getRangeAt(0); var range = document.createRange(); //??? console.log("start container: " + JSON.stringify(highlightRange.startContainer)); console.log("end container: " + JSON.stringify(highlightRange.endContainer)); }
#intro { font-size: 125%; color: green; } p.small { font-size: 75%; } #content { margin-left: 330px; } #buttons { left: 10px; position: fixed; border: solid black 1px; background-color: background:#C0ED72; padding: 5px; width: 300px; } html.ie6 #buttons { position: absolute; } #buttons h3 { font-size: 125%; font-weight: bold; margin: 0; padding: 0.25em 0; }
<div id="buttons"> <h3>Save selection range</h3> <p>Make a selection in the document and use the button below to save the selection range </p> <input type="button" ontouchstart="saveSelectedRange();" onclick="saveSelectedRange();" value="Save selection range"> </div> <div id="content"> <h1>Demo</h1> <p id="intro"> Please use your mouse to make selections from the sample content and use the button on the left to save the selection range. </p> </div>
As you see, console logs empty startContainer
, endContainer
values, but startOffset
, endOffset
properties are ok.如您所见,控制台记录空的
startContainer
、 endContainer
值,但startOffset
、 endOffset
属性没问题。 What values need I to save to be able to restore the selection range in further sessions?我需要保存哪些值才能在以后的会话中恢复选择范围? What are common ways to achieve it?
实现它的常用方法是什么?
Ref: Range class , Selection class参考:范围 class ,选择 class
I have finally solved it by using @TimDown rangy-library
and his module .我终于通过使用@TimDown
rangy-library
和他的模块解决了它。
var highlightRange = window.getSelection().getRangeAt(0);
var serializedRange = rangy.serializeRange(highlightRange);
var deseriazedRange = rangy.deserializeRange(serializedRange);
Important Note: Rangy library creates own RangyRange
class in order to be cross-platformed (I guess), if you want to use methods from DOM Range
class, some of them wont be available, until you setup the rangy
to use Native Range
重要提示: Rangy 库创建了自己的
RangyRange
类以实现跨平台(我猜),如果您想使用DOM Range
类中的方法,其中一些方法将不可用,直到您将rangy
设置为使用Native Range
You can also do it with JavaScript:您也可以使用 JavaScript 进行此操作:
var range;
document.addEventListener("selectionchange", function() {
var selection = document.getSelection();
range = selection.getRangeAt(0);
});
function onClick() {
alert( range.toString() );
window.getSelection().removeAllRanges();
document.getSelection().addRange(range)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.