如何使用JavaScript在webview android中选择文本



我使用这段代码来突出显示android网络视图中的文本。

public void highlight(int pageNumber) {
dbManager = new DBManager(webView.getContext());
dbManager.open();
cursor = dbManager.getSelectedID(books.get(0).getmDbId());
String quotesDb = cursor.getString(cursor.getColumnIndex("_quote"));
String currnetPage = cursor.getString(cursor.getColumnIndex("_current_page"));
List<String> myList = Arrays.asList(quotesDb.split("½_½"));

webView.evaluateJavascript("function doSearch(text, backgroundColor) {n" +
"    if (window.find && window.getSelection) {n" +
"        var windowHeight = window.scrollY;n" +
"        document.designMode = 'on';n" +
"        var sel = window.getSelection();n" +
"        sel.collapse(document.body, 0);n" +
"        while (window.find(text)) {n" +
"            document.execCommand('HiliteColor', false, backgroundColor);n" +
"            sel.collapseToEnd();n" +
"        }n" +
"        document.designMode = 'off';n" +
"        window.scrollTo(0, windowHeight);n" +
"    }n" +
"}", null);

Log.e("SAVE_QUOTE ", "LIST_SIZE ----> " + myList.size());
if (pageNumber == Integer.parseInt(currnetPage)) {
for (int j = 0; j < myList.size(); j++) {
webView.evaluateJavascript("doSearch('" + myList.get(j) + "', 'SkyBlue')", null);
//   webView.findAllAsync(myList.get(j));
}
}
}

在我用新行(段落(选择代码之前,一切都很好。然后我得到一个错误,文本没有突出显示。

/chromium: [INFO:CONSOLE(1)] "Uncaught SyntaxError: Invalid or unexpected token", source: file:///data/user/0/com.mms.tts/cache/W1/index.xhtml (1)

尝试使用jQuery.select((函数

例如

$(#myList.get(j)).select()

其中#myList.get(j(是一个有效的HTML id

注意:您需要参考jQuery库

最新更新