If there is no complex processing such as highlighting, you only need to get the word and position selected in the text, then
Use Window.getselection () to get the selected text content and position in Div
How to get the selected text in textarea
can meet the needs;
--------------------------------------------------------------------------------------------Advanced Requirements------------------------- ----------------------------------------------------------
However, if there is a need for the selected text to be highlighted , use Window.getselection () to get the contents of the selected text and location in the DIV:
<! DOCTYPE html>
There is no problem when the first row is executed, the starting position of the fetch is relative to the DIV tag, and the span tag is added to the HTML after execution
The second time the text after the span label, get the starting position is relative to the span label, there is no method to get the starting position of each is relative to the div?
To see the answer to the same question https://ask.csdn.net/questions/166543, it is useless to try
Here, with a clumsy and ingenious way to add a div to the div#content, and Div#marked-area
<div id= "Content" > <div ng-bind-html= "Markcontentstore" id= "Marked-area-hiden" ></div> <div ng-bind-html= "markcontent" id= "Marked-area" ></div></div>
#content相对定位, #marked-area-hiden absolute positioning, and the background color and font color transparent, floating on the #marked-area,
Note here: #marked-area-hiden and #marked-area position style to a certain, to ensure that two div text position without bias;
Markcontentstore is the markcontent initial value, which is not a highlighted value and remains unchanged
In this way, each choice is actually selected #marked-area-hiden text, highlighting is the text in the #marked-area,
Because the #marked-area-hiden content is always the same, so each time you choose to get the correct location information, and it is transparent, the user sees is the content highlighted in the #marked-area
Use the Window.getselection () method to get the starting and ending positions of the mouse Stroke section (not correctly obtained after highlighting)