简体   繁体   中英

Get selected text from HTML by javascript?

I have a page with formated text and a toolbar (images with click events) beside it. One of the tool-commands is meant to be "mark yellow". (The text has to be read only and is formated.)

I tried to use document.getSelection() and window.getSelection() , but they seem to be designed for input fields only.

The other problem is, that as I click the tool bar my selection gets lost.

How do I get the selected text without loosing the selection?


Solution:

The clue was to use onmousedown="" or jQuery: .mousedown() function to avoid loosing the selection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM