代码之家  ›  专栏  ›  技术社区  ›  Pritish

如何在Web视图中隐藏XML元素后删除空格

  •  0
  • Pritish  · 技术社区  · 7 年前

    我正在开发Android应用程序,对CSS和JavaScript一无所知。我需要在我的WebView中显示一个网页,但是它的元素很少被隐藏。我的以下代码工作成功

     @Override
        public void onLoadResource(WebView view, String url) {
            super.onLoadResource(view, url);
    
          try {
              view.loadUrl("javascript:(function() { " +
                      "document.getElementById('header').style.visibility='collapse';})()");
            /*  view.loadUrl("javascript:(function() { " +
                      "document.getElementById('header').style.visibility='hidden';})()");*/
          }catch (Exception e) {
              e.printStackTrace();
          }
       }
    

    “header”是头的ID,它正在被隐藏,但是它最初在顶部所占的空间仍然存在。所以如何隐藏这个空间。

    谢谢:)

    3 回复  |  直到 7 年前
        1
  •  1
  •   mrdeadsven    7 年前

    使用 display:none display:block

    .style.display='none';

        2
  •  1
  •   user9996625    7 年前

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

        3
  •  1
  •   Liar    7 年前

    我建议使用jsoup:

    public void removeUnusedHTMLTags(org.jsoup.nodes.Document document, String tagClassOrId) {
        Elements categories = document.select(tagClassOrId);
        for (org.jsoup.nodes.Element element : categories){
            Log.v(">>>", "Remove unused tag " + tagClassOrId);
            element.remove();
        }
    }
    

    编辑

    compile 'org.jsoup:jsoup:1.11.3'
    

    Connection con = Jsoup.connect(url)
                    .ignoreContentType(true);
    Connection.Response res = con.execute();
    String rawJSON = res.body();
    Document document = Jsoup.parse(rawJSON);