在Webview Android中隐藏页面上的元素 [英] Hide elements on the page in webview Android

查看:756
本文介绍了在Webview Android中隐藏页面上的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加载站点,但不想显示divHeader,我已经尝试了几种方法,显然我使用的代码没有错误,因为logcat中没有消息...如何隐藏特定的Div.

I want to load the site , but do not want to show divHeader , I've tried several ways and apparently the code I'm using does not have mistakes because there are no messages in logcat ... How do I hide a particular Div.

package br.webview.com;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;   


    public class WebViewDevMobileActivity extends Activity {

        private WebView myWebView;

        private final String URL = "http://uniceu.prefeitura.sp.gov.br/";

        @SuppressLint("SetJavaScriptEnabled")
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.site);

            if (!DetectConnection.checkInternetConnection(this)) {

                  Toast.makeText(getApplicationContext(), "Opa, parece que vc esta sem internet!", Toast.LENGTH_LONG).show();
                } else {  

            myWebView = (WebView) findViewById(R.id.webView1);
            myWebView.getSettings().setJavaScriptEnabled(true);         
            myWebView.setWebViewClient(new WebViewClient()
            {  
                @Override  
                public void onPageFinished(WebView view, String url)  
                {  
                    myWebView.loadUrl("javascript:(function() { " +  
                            "document.getElementById('divHeader')[0].style.display='none'; " +  
                            "})()");  
                }  
            });  

            myWebView.loadUrl(URL);
            myWebView.getSettings().setBuiltInZoomControls(true);
        }
        }

        @Override
        public void onBackPressed() {
            if(myWebView.canGoBack()) {
                myWebView.goBack();
            } else {
                super.onBackPressed();
            }    
}   
    }

推荐答案

getElementById()返回单个Node元素.试试:

getElementById() returns single Node element. Try:

document.getElementById('divHeader').style.display='none'

要操作多个元素,最好将其他类添加到应隐藏在WebView中的元素(在本例中为__hiddenInWebView),然后:

To manipulate multiple elements would be better to add additional class to elements that should be hidden in WebView (__hiddenInWebView in this case), and then:

var items = document.querySelectorAll('.__hiddenInWebView');
for (index in items) {
    items[index].style.display = 'none';
}

如果您无法从网页上修改DOM(添加其他类),然后用元素填充数组并将其隐藏在循环中:

if You can't modify DOM from webpage (add additional classes), then populate array with elements and hide them in a loop:

var items = [];
items.push(document.querySelector('#first'));
items.push(document.querySelector('#second'));
for (index in items) {
    items[index].style.display = 'none';
}

这篇关于在Webview Android中隐藏页面上的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆