从许多相同的文本中查找选定的文本

时间:2014-12-29 15:48:21

标签: jquery javascript-events javascript

我想要做的是我想为我的项目创建一个在线书签应用程序。我想从HTML模板中选择元素或段落或文本,想要为模板的选定部分指定特定颜色,然后将其与当前用户,选定颜色和当前URL一起存储在数据库中,以及下次使用时选择文本的用户访问同一页面时,相同的文本以用户选择的相同颜色显示。

例如:我有一个模板,其中我有一些段落,我从第二段选择一个名为Java Script的单词。那里重复了java脚本这个词。

JavaScript was originally developed by Brendan Eich, while working for Netscape   
Communications Corporation. While competing with Microsoft for user adoption of web  
technologies and platforms, Netscape considered their client-server offering a
distributed OS with a portable version of Sun Microsystems' Java providing an
environment in which applets could be run.

JavaScript was originally developed by Brendan Eich, while working for Netscape
Communications Corporation. While competing with Microsoft for user adoption of web
technologies and platforms, Netscape considered their client-server offering a
distributed OS with a portable version of Sun Microsystems' Java providing an
environment in which applets could be run.

JavaScript was originally developed by Brendan Eich, while working for Netscape 
Communications Corporation. While competing with Microsoft for user adoption of web
technologies and platforms, Netscape considered their client-server offering a
distributed OS with a portable version of Sun Microsystems' Java providing an
environment in which applets could be run.

我能以某种方式找出我选择的java script字。 DOM是否提供了这样的操作,以便从哪个段落中选择一个单词? 帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

您可以参考this StackOverflow question上的答案,了解如何获取所选文字的范围。然后,使用startOffset和endOffset Range属性,您可以在数据库中保存所选文本的开始和结束索引,以便将来重新选择此文本并使用它创建一个新的样式化DOM节点,一些彩色背景或类似的东西。