只是必须这样做,所以有一些可行的解决方案(可能需要进行一些测试)
基本思想是:
使用此方法获取 textContent 位置:在包含 HTML 内容的 contentEditable 区域中获取插入符号(光标)位置
遍历元素的 innerHTML 到 textContent 位置
如果遇到 html 标签或实体,遍历它直到正常字符,然后继续
示例代码在这里:
function getCaretPosition (node) {
var range = window.getSelection().getRangeAt(0),
preCaretRange = range.cloneRange(),
caretPosition,
tmp = document.createElement("div");
preCaretRange.selectNodeContents(node);
preCaretRange.setEnd(range.endContainer, range.endOffset);
tmp.appendChild(preCaretRange.cloneContents());
caretPosition = tmp.innerHTML.length;
return caretPosition;
}
function getHTMLCaretPosition(element) {
var textPosition = getCaretPosition(element),
htmlContent = element.innerHTML,
textIndex = 0,
htmlIndex = 0,
insideHtml = false,
htmlBeginChars = ['&', '<'],
htmlEndChars = [';', '>'];
if (textPosition == 0) {
return 0;
}
while(textIndex < textPosition) {
htmlIndex++;
// check if next character is html and if it is, iterate with htmlIndex to the next non-html character
while(htmlBeginChars.indexOf(htmlContent.charAt(htmlIndex)) > -1) {
// console.log('encountered HTML');
// now iterate to the ending char
insideHtml = true;
while(insideHtml) {
if (htmlEndChars.indexOf(htmlContent.charAt(htmlIndex)) > -1) {
if (htmlContent.charAt(htmlIndex) == ';') {
htmlIndex--; // entity is char itself
}
// console.log('encountered end of HTML');
insideHtml = false;
}
htmlIndex++;
}
}
textIndex++;
}
//console.log(htmlIndex);
//console.log(textPosition);
// in htmlIndex is caret position inside html
return htmlIndex;
}