【发布时间】:2013-02-07 12:29:02
【问题描述】:
我的要求是获取用户选择的文本相对于主 div 的开始和结束字符索引,比如说“内容”div。请参阅下面的示例代码。 但是当我在网络视图中选择“丰富”一词时。它的开始和结束字符索引分别对应于“span”标签的位置,即1和5。而它应该是 12 和 16 w.r.t “内容” div。
<html>
<body>
<div id="content" contenteditable="true" style="font-family: Times New Roman; color: #fff; font-size: 18;">
This is <span style="background-color: yellow;">out</span> Rich Text Editing View
</div>
</body>
目前使用的JavaScript函数m是
function getHighlightedString()
{
var text = document.getSelection();
startIndex = text.anchorOffset;
endIndex = text.focusOffset;
selectedText = text.anchorNode.textContent.substr(startIndex, endIndex - text.anchorOffset);
}
请帮帮我。
【问题讨论】:
标签: javascript iphone html ios uiwebview