Is it possible to get the highlighted text in a paragraph of a website e.g. by using jQuery?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
- jQuery add and remove delay
Get highlighted text this way:
and of course a special treatment for ie:
Getting the text the user has selected is relatively simple. There's no benefit to be gained by involving jQuery since you need nothing other than the
window
anddocument
objects.If you're interested in an implementation that will also deal with selections in
<textarea>
and texty<input>
elements, you could use the following. Since it's now 2016 I'm omitting the code required for IE <= 8 support but I've posted stuff for that in many places on SO.This solution works if you're using chrome (can't verify other browsers) and if the text is located in the same DOM Element: