我需要使用 Java 和 Jsoup 決議表格并根據其值更改單元格的顏色。這是 html 表格的樣子,也是需要定義單元格顏色的方式
<tr>
<td colspan="1">Test1</td>
<td colspan="1">JD</td>
<td colspan="1">N</td>
<td colspan="1">A</td>
</tr>
<tr>
<td>Test2</td>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<tr>
<td class="highlight-#fff0b3" title="Background color : Light yellow 100%" data-highlight-colour="#fff0b3">COLOR YELLOW</td>
<td class="highlight-#fff0b3" title="Background color : Light yellow 100%" data-highlight-colour="#fff0b3">YELL</td>
<td class="highlight-#fff0b3" title="Background color : Light yellow 100%" data-highlight-colour="#fff0b3">N/A</td>
<td class="highlight-#fff0b3" title="Background color : Light yellow 100%" data-highlight-colour="#fff0b3">N/A</td>
</tr>
我撰寫了一個腳本,可以準備單元格的值并從中更改文本,但是我也無法更改顏色
Document doc = Jsoup.parse(html);
Elements rows = doc.getElementsByTag("tr");
String status = "Pass"
for(Element row : rows) {
String Column1 = row.getElementsByTag("td").get(0).text();
if(Column1 == "MyValue"){
row.getElementsByTag("td").get(1).text("CustomValue");
row.getElementsByTag("td").get(2).text("CustomValue");
row.getElementsByTag("td").get(3).text("SomeValue");
if(status == "Pass"){ // Everything below doesn't work
row.getElementsByTag("td").get(1).class("highlight-#57d9a3");
row.getElementsByTag("td").get(2).class("highlight-#57d9a3");
row.getElementsByTag("td").get(3).class("highlight-#57d9a3");
row.getElementsByTag("td").get(1).title("Background color : Medium green 65%");
row.getElementsByTag("td").get(2).title("Background color : Medium green 65%");
row.getElementsByTag("td").get(3).title("Background color : Medium green 65%");
} else if(status == "Fail"){
row.getElementsByTag("td").get(1).class("highlight-#ff7452");
row.getElementsByTag("td").get(2).class("highlight-#ff7452");
row.getElementsByTag("td").get(3).class("highlight-#ff7452");
row.getElementsByTag("td").get(1).title("Background color : Medium red 85%");
row.getElementsByTag("td").get(2).title("Background color : Medium red 85%");
row.getElementsByTag("td").get(3).title("Background color : Medium red 85%");
} else{
//TBD
}
}
這是我得到的錯誤:
No signature of method: org.jsoup.nodes.Element.class() is applicable for argument types: (java.lang.String) values: [highlight-#57d9a3]
10:44:33 Possible solutions: clone(), clone(), addClass(java.lang.String), hasClass(java.lang.String), val(java.lang.String), getClass()
uj5u.com熱心網友回復:
row.getElementsByTag("td").get(1)回傳一個Element,它沒有class方法。
不過,它有一個classNames方法,并且接受 aSet<String>作為引數(即使您只需要傳遞一個類)。
所以你可以做類似的事情
row.getElementsByTag("td").get(1).classNames(Set.of("highlight-#57d9a3"));
等等。
除了那個編譯錯誤,if(status == "Pass") 不是你如何比較 java 中的字串。你需要做類似的事情if("Pass".equals(status))。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/510514.html
標籤:爪哇汤
