机器人的WebView:突出使用JavaScript的网页的特定词?机器人、突出、网页、WebView

2023-09-12 22:31:41 作者:染指旳沵唇。

我用在我的Andr​​oid应用程序中的web视图。我想知道是否有可能突出或使用在javascript code做?强调一个特定的单词/句子/段落加载的页面

i am using a webview inside my android application. I would like to know if it is possible to highlight or underline a specific word/sentence/paragraph in a loaded page using code done in javascript?

感谢

推荐答案

您应该看看此页。做一个视图源。

You should look at this page. Do a view source.

他们用纯Javascript的 - 即使不是jQuery的 - 突出网页上的特定词。

They use pure Javascript--not even jQuery--to highlight particular words on the webpage.

他们基本上加载所有他们想通过搜索到一个变量中,不断找到他们想要突出的indexOf 搜索词的文字。当他们发现这个词的索引,他们建立一个新的字符串与索引之前的一切+ A <跨度类=高亮> +搜索词+ < / SPAN> 。然后,他们继续搜索。一旦他们已经搜索并重建所有的文字替换它们与新的文本DOM中的旧文本。

Basically they load all of the text they want to search through into a variable and continuously find the indexOf the search term they want to highlight. When they find the index of the term, they build a new string with everything before that index + a <span class="highlighted"> + the search term + </span>. Then they continue searching. Once they've searched and rebuilt all the text they replace the old text in the DOM with the new text.

他们也有沿 .highlighted {background-color的线条一些CSS:黄色; }