我在 Activity 中使用 webview 来显示网页,并使用 javascript 隐藏页眉。
我在 chrome 控制台中尝试了以下脚本,它工作正常:document.getElementsByClassName('Header')[0].style.display = 'none';
当我在 android webview 中使用相同的脚本时,页面被清除并显示 none
这是脚本的输出。 (也在 Chrome 控制台上收到)。
String s = (new StringBuilder())
.append(" javascript: document.getElementsByClassName('Header')[0].style.display = 'none';")
.toString();
webView.loadUrl(s);
最佳答案
你可以使用下面的代码 -
try {
// Load the html into jsoup
Document doc = Jsoup.connect("http://your-site.com/").get();
// find and remove header
Element header = doc.getElementById("your-header");
header.remove();
// find and remove footer
Element footer = doc.getElementById("your-footer");
footer.remove();
// Load data into a WebView
WebView wv = (WebView) findViewById(R.id.webView);
WebSettings ws = wv.getSettings();
ws.setJavaScriptEnabled(true);
wv.loadData(doc.toString(), "text/html", "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
您将在 this link 找到最新的 Jsoup 库
.
可以通过添加以下依赖项将库添加到 gradle compile 'org.jsoup:jsoup:1.8.2'
关于javascript - 使用 loadUrl() 在 android webview 中加载 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31378175/