javascript Android webview:使用javascript突出显示页面中的特定单词?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5294562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android webview: highlight a specific word in a page using javascript?
提问by shadesco
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?
我在我的 android 应用程序中使用 webview。我想知道是否可以使用在 javascript 中完成的代码在加载的页面中突出显示或下划线特定的单词/句子/段落?
thanks
谢谢
采纳答案by Chris W.
You should look at this page. Do a view source.
你应该看看这个页面。做一个查看源。
They use pure Javascript--not even jQuery--to highlight particular words on the webpage.
他们使用纯 Javascript——甚至不是 jQuery——来突出显示网页上的特定单词。
Basically they load all of the text they want to search through into a variable and continuously find the indexOfthe 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.
基本上,他们将想要搜索的所有文本加载到一个变量中,并不断找到indexOf他们想要突出显示的搜索词。当他们找到该词的索引时,他们会用该索引之前的所有内容 + a <span class="highlighted">+ 搜索词 +构建一个新字符串</span>。然后他们继续寻找。一旦他们搜索并重建了所有文本,他们就会用新文本替换 DOM 中的旧文本。
They also have some CSS along the lines of .highlighted { background-color: yellow; }
他们也有一些 CSS .highlighted { background-color: yellow; }
回答by Ashok Goli
Why can't you try this? titleWebView is the WebView that has the html loaded or to be loaded. searchText should contain the text to be searched. I constructed a dialog to get the search terms from the user and reloaded the webpage each time the user hits the search. All the terms containing the searchText is highlighted (light green on my Nexus One). Hope this helps.
你为什么不能试试这个?titleWebView 是已加载或要加载 html 的 WebView。searchText 应包含要搜索的文本。我构建了一个对话框来从用户那里获取搜索词,并在用户每次点击搜索时重新加载网页。包含 searchText 的所有术语都突出显示(在我的 Nexus One 上为浅绿色)。希望这可以帮助。
titleWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
if (searchText != null && !searchText.equals("")) {
int i = titleWebView.findAll(searchText);
Toast.makeText(getApplicationContext(), "Found " + i + " results !",
Toast.LENGTH_SHORT).show();
try {
Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE);
m.invoke(titleWebView, true);
} catch (Throwable ignored) {
}
searchText = "";
}
}
});
回答by Uncaught Exception
findAll() is deprecated! You can use findAllAsync() from API16 onwards in a simple manner like this:
findAll() 已弃用!从 API16 开始,您可以通过如下简单方式使用 findAllAsync():
webView.setWebViewClient(new WebViewClient(){
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onPageFinished(WebView view, String url) {
if (searchText != null && !searchText.equals("")) {
webView.findAllAsync(searchText);
}
}
});
回答by Youddh
Code
代码
Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE);
m.invoke(titleWebView, true);
After this test using following code.
使用以下代码进行此测试后。
findAll("search text")
When text is match Toastdisplay.
当文本匹配Toast显示时。

