我想借助此回答Display a part of the webpage on the webview android
从网页中删除“标题”元素但是我收到一条错误消息;请参阅此https://dl.dropboxusercontent.com/u/15065300/Check-2.jpg
这是我的完整代码:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url)
{
webView.loadUrl("javascript:(function() { " +
"document.getElementsByTagName('myDiv1')[0].style.display="none"; " +
"})()");
}
});
webView.loadUrl("http://skyasim.info/abc.html");
}
}
同时显示权限
<uses-permission android:name="android.permission.INTERNET" />
答案 0 :(得分:6)
您需要逃离"
。请参阅Escape Sequences部分。
webView.loadUrl("javascript:(function() { " +
"document.getElementsByTagName('header')[0].style.display=\"none\"; " +
"})()");
答案 1 :(得分:2)
这对我有用:
webView.loadUrl("javascript:var con = document.getElementById('a'); " +
"con.parentNode.removeChild(con); ");
htmlCode:
<ul>
............
<div id="a">
<H4 align="center"> something</H4>
</div>
</ul>
答案 2 :(得分:0)
<div class="row"></div>
webview.loadUrl("javascript:if (typeof(document.getElementsByClassName('row')[0]) != 'undefined' && document.getElementsByClassName('row')[0] != null){document.getElementsByClassName('row')[0].style.display = 'none';} void 0");