【问题标题】:Load JavaScript in android webview using loadUrl()使用 loadUrl() 在 android webview 中加载 JavaScript
【发布时间】:2015-10-01 09:51:40
【问题描述】:

我在我的活动中使用 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);

【问题讨论】:

    标签: javascript android android-webview


    【解决方案1】:

    您可以使用以下代码 -

        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 Library

    该库可以通过添加以下依赖compile 'org.jsoup:jsoup:1.8.2'来添加到gradle

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多