Use the Window.getselection () method to get the starting and ending positions of the mouse Stroke section (not correctly obtained after highlighting)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.