安卓 WebView 隐藏 div

4
我希望在Android的WebView上部分显示网页,并从网页中删除某些div元素。我有一个像下面这样的网页:
    <!DOCTYPE html>
<head></head>
<body>
<div id="container">
<div id="header"></div>
<div id="main"></div>
</div>
</body></html>

我尝试了像这样的方法,但页面上只显示"none"这个标题或者页面加载后只显示白色背景。请问有人有什么想法吗?

view.loadUrl("javascript:document.getElementById('header').style.display = 'none';")

这是我的Java代码。

    package com.example.client6.tetrapolis;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


public class lichkab extends Activity {

public class HelloWebViewClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:var con = document.getElementById('header');" +"con.style.display = 'none'; ");
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lichkab);
//
mWebView = (WebView) findViewById(R.id.webview);
//
mWebView.getSettings().setJavaScriptEnabled(true);
//
mWebView.loadUrl("#");
mWebView.getSettings().setLoadsImagesAutomatically(false);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.setWebViewClient(new HelloWebViewClient());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.lichkab, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
WebView mWebView;
}
2个回答

8

我遇到了同样的问题,所以我将 JavaScript 修改为以下代码:

view.loadUrl("javascript:document.getElementById(\"header\").setAttribute(\"style\",\"display:none;\");");

希望这能有所帮助。 再见!


我收到了许多 I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot read property 'setAttribute' of null", source: my_long_url_is_here (1) 的信息。 - Someone Somewhere
我已经想通了:就在 onPageFinished() 方法中,HTML 是加密的。通过使用 webView.postDelayed(new Runnable(javascript)) 延迟 1 秒钟,然后 HTML 就可以使用了。 - Someone Somewhere

1

注意:使用不同于我的代码的class隐藏id时,它可以正常工作:

当使用id时,我遇到了相同的问题:

view.loadUrl("javascript:document.getElementById(\"header\").setAttribute(\"style\",\"display:none;\");");

当使用class时,我遇到了相同的问题:
view.loadUrl("javascript:document.getElementsByClassName('footer')[0].style.display='none';");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接