我正在使用下面的代码来获取和存储单词的开始和结束位置现在我想在刷新页面时突出显示这些单词..怎么做。
function getSelectionCharOffsetsWithin(element)
{
var start = 0, end = 0;
var sel, range, priorRange;
if (typeof window.getSelection != "undefined") {
range = window.getSelection().getRangeAt(0);
priorRange = range.cloneRange();
priorRange.selectNodeContents(element[0]);
priorRange.setEnd(range.startContainer, range.startOffset);
start = priorRange.toString().trim().length;
end = start + range.toString().length;
} else if (typeof document.selection != "undefined" &&
(sel = document.selection).type != "Control") {
range = sel.createRange();
priorRange = document.body.createTextRange();
priorRange.moveToElementText(element);
priorRange.setEndPoint("EndToStart", range);
start = priorRange.text.length;
end = start + range.text.length;
}
return {
start: start,
end: end
};
}