Getting an exact reference for a selected text in HTML

Let's assume that I have the following text within a single <p>... </p>

When a user selects a couple of words within the text, lets assume the first Quisque vestibulum is selected, I get the selected text with window.getSelection().toString(); then I apply some CSS tricks, lets say I underline the text.

When the user comes back one month later, I would like to underline same text again. How can I get an exact reference for the selected text for future reference? I don't want to highlight the second Quisque vestibulum .

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum at dolor sit amet augue tempor venenatis vitae et nibh. Donec dignissim fringilla fermentum. In facilisis consectetur risus. Nam vel mauris magna, a feugiat nisl. Quisque vestibulum, neque eu tristique tristique, tortor mauris ornare magna, at ultricies augue orci sed enim. Nulla magna ligula, dapibus vel commodo ac, sodales ut sem. Duis lorem massa, consectetur sed molestie eget, posuere eu magna. Pellentesque sollicitudin mattis facilisis. Nunc pulvinar metus et diam dignissim sit amet pellentesque metus pretium. Quisque vestibulum, maecenas vel leo sit amet diam iaculis sollicitudin.


您可以使用search()或indexOf()并存储字符串的位置以及实际选定的文本。

链接地址: http://www.djcxy.com/p/88582.html

上一篇: 如何在网页的特定位置创建永久链接?

下一篇: 获取HTML中选定文本的确切参考