我已经结束了正是如此解决它来处理所有情况:
var range = sel.getRangeAt(0);
// inserts two spans at the beginning and end of the range, to
// calculate the offsets
var div = document.createElement("span");
div.setAttribute("class", START_NODE);
range.insertNode(div);
range.collapse(false);
div = document.createElement("span");
div.setAttribute("class", END_NODE);
range.insertNode(div);
// gets the offsets by looking up the location of the inserted spans
// removes them after finding the offsets (also so the starting
// offset won't screw up the ending offset)
comment.startOffset = p.html().indexOf('');
$("." + START_NODE).remove();
comment.endOffset = p.html().indexOf('');
$("." + END_NODE).remove();
p.html(p.html());
基本上,我在范围选择开始时添加了一个SPAN,然后折叠范围并在其末尾添加一个SPAN。 然后我只搜索我添加的第一个跨度的索引,以及我添加的第二个跨度的索引以找到偏移量。