我得到了以下HTML代码:
<div id="editable_phrase">
My
<span style="background-color: #19FC49;"> first</span>
<span style="background-color: #09AC20;"> first</span> //note 2 firsts here
<span style="background-color: #D2C3EA;"> phrase</span>
</div>
我需要找到原始短语中所选单词的首字母位置。因此,如果我选择(用鼠标突出显示)phrase
,则需要获取10(或它实际上的位置)。我尝试使用charAt()
或window.getSelection
但仍然得到错误的结果。
此外,如果我选择同一个单词但在不同位置(就像上面我的示例中有2个first
单词),我将得到相同的结果,因为它找到了第一个单词的位置,而我需要第二个单词的位置。
我可以使用jQuery。任何想法都会受到欢迎。谢谢。