window.getSelection()
将为您提供当前选择点,然后从此处向后树遍历直到您的节点开始,添加遇到的所有文本节点的长度。
function walkback(node, stopAt) {
if (node.childNodes && node.childNodes.length) { // go to last child
while (node && node.childNodes.length > 0) {
node = node.childNodes[node.childNodes.length - 1];
}
} else if (node.previousSibling) { // else go to previous node
node = node.previousSibling;
} else if (node.parentNode) { // else go to previous branch
while (node && !node.previousSibling && node.parentNode) {
node = node.parentNode;
}
if (node === stopAt) return;
node = node.previousSibling;
} else { // nowhere to go
return;
}
if (node) {
if (node.nodeType === 3) return node;
if (node === stopAt) return;
return walkback(node, stopAt);
}
return;
}
function getRealCaretPosition() {
var sel = window.getSelection(), // current selection
pos = sel.anchorOffset, // get caret start position
node = sel.anchorNode; // get the current #text node
while (node = walkback(node, myContentEditableElement)) {
pos = pos + node.data.length; // add the lengths of the previous text nodes
}
return pos;
}
当然,您还需要检查当前选择是否确实在您感兴趣的HTMLElement中。