我正在尝试删除webview中的HTML元素。这是我的代码。一切都很好。
我已经查看了其他问题,但没有一个答案适合我。
// load web url
final WebView webView = (WebView) mRootView.findViewById(R.id.fragment_main_webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript:(function() { " + "document.getElementsByTagName('publicTabs')[0].style.display=\"none\"; " + "})()");
}
});
webView.loadUrl(mUrl);
当我在手机上打开应用时,没有网页加载。只是空白。如果我删除:
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript:(function() { " + "document.getElementsByTagName('publicTabs')[0].style.display=\"none\"; " + "})()");
}
});
网页加载。
另外,有人可以告诉我如何同时删除多个HTML元素吗?
任何帮助都将不胜感激。
答案 0 :(得分:0)
下面是答案:还要检查元素是网站中的类还是ID。就像我的代码中的下面一样。
String url = "https://www.example.com";
final WebView webView = (WebView) findViewById(R.id.fragment_main_webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.getSettings().setSavePassword(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setSaveFormData(true);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setSupportMultipleWindows(false);
webView.getSettings().setLightTouchEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript:(function() { " +
"document.getElementsByClassName('header home_header')[0].style.display='none'; " +
"document.getElementsByClassName('glyphicon glyphicon-menu-hamburger')[0].style.display='none'; " +
"document.getElementsByClassName('searchBottomLinks_index searchBottomLinks_internal')[0].style.display='none'; " +
"document.getElementsByClassName('list-unstyled list-inline')[0].style.display='none'; " +
"document.getElementsByClassName('row')[0].style.display='none'; "+
"document.getElementsByClassName('header home_header')[0].style.display='none'; " +
"document.getElementsById('header-inner')[0].style.display='none'; " +
"document.getElementsById('fixed-header')[0].style.display='none'; " +
"document.getElementsByClassName('collapse navbar-collapse')[0].style.display='none'; " +
"document.getElementsByClassName('navbar navbar-inverse sidebars')[0].style.display='none'; " +
"document.getElementsByClassName('footer')[0].style.display='none'; " +
"document.getElementsByClassName('footer2')[0].style.display='none'; " +
"document.getElementsByClassName('container')[0].style.display='none'; " +
"document.getElementsByClassName('acad-slider')[0].style.display='none'; " +
"})()");
}
});
webView.loadUrl("https://www.example.com");
}
}
答案 1 :(得分:0)
我遇到了同样的问题,并通过在javascript行之前添加“ var x =“来解决了该问题:
webView.loadUrl("javascript:var x = document.getElementsByClassName('header')[0].style.display='none';");