Selected Text Instance
I have a bunch of text on my document. You can select word/words from it and highlight the text. After you highlight it, an ajax request is sent and the selected word/words are sent to backend (PHP) and processed to be saved into the database. The page is refreshed and the selected text is permanently higlighted.
Now the bad part, if you higlight "the", it will highlight all the "the"'s in the text document. How do I detect which "the" is higlight开发者_StackOverflow中文版ed through javascript so that only the proper instance is saved in database.
You can store word offsets instead of words. For example for the string 'blabla the bla', you would store '8,3' (offset, length). However, you'll have to recalculate all the offsets in the document if it changes.. (fabrik is right)
You can store selected text's starting and ending characters position relative to whole document. (of course this approach will work only when your document doesn't changing).
Another approach to figure out which word should be highlighted is by it's context -- this may also be wrong sometimes but needs less calculations. Or position (like 3rd "the", 7th "the").
精彩评论