我的问题是 jsoup 破坏了我的 html 代码。我正在从 url 下载网站,稍微更改一下并在 WebView 中显示它。
Jsoup 在代码中添加格式化并导致问题:
在html之前:http: //jsfiddle.net/zr3xtus9/
<div class="ctr-element"><div class="ctr-content" style="z-index: 100; margin: 0px; left: 0px; top: 0px; visibility: visible; position: relative;"><div class="ctr-textWrapper" style="text-align: center; line-height: 1; font-size: 124px;" spellcheck="false"><span style="font-size: 124px; color: rgb(245, 230, 93); font-family: capture_it_2;" id="object13textContainer0"> P</span><span style="font-size: 124px; color: rgb(245, 230, 93); font-family: capture_it_2;" id="object13textContainer1">arty</span></div></div></div>
Run Code Online (Sandbox Code Playgroud)
html之后:http: //jsfiddle.net/5kv47sny/
<div class="ctr-element">
<div class="ctr-content" style="z-index: 100; margin: 0px; left: 0px; top: 0px; visibility: visible; position: relative;">
<div class="ctr-textWrapper" style="text-align: center; line-height: 1; font-size: 124px;" spellcheck="false">
<span style="font-size: 124px; color: rgb(245, 230, 93); font-family: capture_it_2;" id="object13textContainer0" onclick="javscript:changeText('object13textContainer0')"> P</span>
<span style="font-size: 124px; color: rgb(245, 230, 93); font-family: capture_it_2;" id="object13textContainer1" onclick="javscript:changeText('object13textContainer1')">arty</span>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,jsoup 添加了额外的空格作为文本缩进。如何避免这种情况?
解决办法很简单。为了禁用文本缩进,我在打印 html 之前使用了以下代码:
doc.outputSettings().prettyPrint(false);
Run Code Online (Sandbox Code Playgroud)
这是文档:http://jsoup.org/apidocs/org/jsoup/nodes/Document.OutputSettings.html#prettyPrint(boolean)