How can I use Javascript to ge开发者_JAVA百科t the position of highlighted words in an HTML file?
Try looking into window.getSelection
: https://developer.mozilla.org/en/DOM/selection
This is for Firefox though.
You will need to do research to make it cross-browser.
Maybe there is a open-source library that does this.
You can use Rangy (http://code.google.com/p/rangy).
A cross-browser JavaScript range and selection library. It provides a simple standards-based API for performing common DOM Range and Selection tasks in all major browsers, abstracting away the wildly different implementations of this functionality between Internet Explorer and DOM-compliant browsers.
(Disclaimer: This answer is promoting the comment from the author that library to an answer, where it belongs)
精彩评论