【问题标题】:How to add padding to html content using Jsoup?如何使用 Jsoup 为 html 内容添加填充?
【发布时间】:2016-09-05 09:17:02
【问题描述】:

我正在使用 Jsoup 库来解析 HTML 内容。我正在使用以下代码为我的代码标签添加样式。

                Document doc = Jsoup.parse(html_open);
            doc.append(content);
            doc.append(html_close);
            webView1.getSettings().setJavaScriptEnabled(true);
            webView1.setInitialScale(getScale());
            webView1.getSettings().setAllowFileAccess(true);
            webView1.getSettings().setLoadsImagesAutomatically(true);
            webView1.setWebViewClient(
                    new WebViewClient() {

                        @Override
                        public void onPageFinished(WebView view, String url) {
                            String javascriptCode = "javascript:";
                            javascriptCode+="var elements=document.querySelectorAll(\"code\");";
                            javascriptCode+="var parent;";
                            javascriptCode+="var container;";
                            javascriptCode+="for(i in elements){";
                            javascriptCode+="container = document.createElement('div');";
                            javascriptCode+="parent = elements[i].parentElement;";
                            javascriptCode+="parent.replaceChild(container, elements[i]);";
                            javascriptCode+="container.appendChild(elements[i]);";
                            javascriptCode+="container.setAttribute(\"style\",\" white-space: nowrap; background-color: #EEEEEE;  padding-top: 4px;  padding-right: 4px;  padding-bottom: 4px;  padding-left: 4px;\");}";
                            webView1.loadUrl(javascriptCode);
                        }
                    }
            );
            webView1.loadDataWithBaseURL("", String.valueOf(doc), "text/html", "UTF-8", "");

这是我要解析的 html 代码。

<h2>Checking Data Features</h2>
<h3>Get the names of the columns of an object</h3>
<p><code><strong>names</strong>(salary_data)</code></p>
<p><code>&gt; names(salary_data)</code> <code>[1] "First_Name" "Last_Name" "Grade" <br />[4] "Location" "ba" "ms"</code></p>
<h3>Compactly display the internal structure of an R Object</h3>
<p><code><strong>str</strong>(salary_data)</code></p>
<p><code>&gt; <strong>str</strong>(salary_data)</code></p>
<p><code>'data.frame': 12 obs. of 6 variables:</code></p>
<p><code>&nbsp;$ First_Name: Factor w/ 12 levels "Aaron","Adela",..: 4 3 10 5 9 11 1 8 12 7 ... <br />&nbsp;$ Last_Name : Factor w/ 12 levels "Brown","Chavan",..: 1 12 5 6 8 2 3 7 4 10 ... <br />&nbsp;$ Grade : Factor w/ 2 levels "GR1","GR2": 1 2 1 2 1 2 1 2 1 2 ... <br />&nbsp;$ Location : Factor w/ 2 levels "DELHI","MUMBAI": 1 2 2 1 2 2 2 2 1 1 ... <br />&nbsp;$ ba : int 17990 12390 19250 14780 19235 13390 23280 13500 20660 13760 ... <br />&nbsp;$ ms : int 16070 6630 14960 9300 15200 6700 13490 10760 NA 13220 ...</code></p>
<p>Structure gives the following information:</p>
<ul>
<li>Type of the variable.</li>
<li>Number of levels of each factor.</li>
<li>Some values of the first few rows</li>
</ul>

我正在尝试为我的 html 中的代码标签添加边框、背景颜色和填充。但是这段代码没有在我的html内容中添加所需的样式。我在android web view中显示这个内容。有没有办法在div中添加所有代码标签,然后为div设置样式。

我的输出看起来像:-

感谢任何帮助或建议。谢谢。

【问题讨论】:

    标签: android html css android-webview jsoup


    【解决方案1】:

    您可以使用 javascript 设置样式。在webView1.loadDataWithBaseURL(...)前添加如下代码

    webView1.setWebViewClient(
        new WebViewClient() {
    
            @Override
            public void onPageFinished(WebView view, String url) {
                String javascriptCode = "javascript:";
                javascriptCode+="var elements=document.querySelectorAll(\"code\");";
                javascriptCode+="var parent;";
                javascriptCode+="var container;";
                javascriptCode+="for(i in elements){";
                javascriptCode+="container = document.createElement('div');";
                javascriptCode+="parent = elements[i].parentElement;";
                javascriptCode+="parent.replaceChild(container, elements[i]);";
                javascriptCode+="container.appendChild(elements[i]);";
                javascriptCode+="container.setAttribute(\"style\",\"overflow:scroll; border: 1px solid black; background-color: #EEEEEE;  padding-top: 50px;  padding-right: 30px;  padding-bottom: 50px;  padding-left: 80px;\");}";
                webView1.loadUrl(javascriptCode);
            }
        }
    );
    

    【讨论】:

    • 这可行,但它为代码标签内的每一行添加了样式。我可以将样式添加到整个代码块中吗?
    • 你能添加一个html结构的例子吗?这将简化流程。
    • 请检查我更新的问题,为了清晰起见,我还添加了一个屏幕截图。谢谢。
    • 查看更新的答案:将代码包装在一个新元素中(这里例如一个 div)并将样式应用于新元素。经过如此测试,似乎可以正常工作。不过必须添加overfolw:scroll
    • 我希望 padding-right 成为字符串的长度。我怎样才能做到这一点?我尝试使用此代码,但它对我不起作用 javascriptCode+="container.setAttribute(\"style\",\" white-space: nowrap; background-color: #EEEEEE; padding-top: 4px; padding-right:元素[i].length; padding-bottom: 4px; padding-left: 4px;\");}";
    猜你喜欢
    • 2014-01-21
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    相关资源
    最近更新 更多