Java 将td元素添加到rowspan td的下一个tr

Java 将td元素添加到rowspan td的下一个tr,java,html,jsoup,Java,Html,Jsoup,我有下面的html <!DOCTYPE html> <html> <body> <table border="1"> <tr> <th>Month</th> <th>Savings</th> <th>Savings for holiday!</th> </tr> <tr> <td>Ja

我有下面的html

<!DOCTYPE html>
<html>
<body>

<table border="1">
  <tr>
    <th>Month</th>
    <th>Savings</th>
    <th>Savings for holiday!</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
    <td rowspan="2">$50</td>
  </tr>
  <tr>
    <td>February</td>
    <td>$80</td>
  </tr>
</table>

</body>
</html>

可能提示:对于条件

cell.hasAttr("rowspan")
获取行索引,如

int index = row.getIndex();
然后通过索引+1得到下一行,如

Element eRow = rows.get(index+1);

然后将td元素追加到此行,这将是您在rowspan行中的下一行。

您只需使用以下代码即可追加此行:

Elements rows = table.select("tr > td[rowspan=2]");

for (Element row : rows) {
    row.parent().nextElementSibling().append("<td>$50</td>");
}
Elements行=表。选择(“tr>td[rowspan=2]”;
用于(元素行:行){
row.parent().nextElementSibling().append($50”);
}

对所有内容进行编码后,我找到了解决方案。下面是代码

for (Element row : rows) {
        int cellIndex = -1;
        if(row.select("td").hasAttr("rowspan")){
            for (Element cell : row.select("td")) {
                cellIndex++;
                if (cell.hasAttr("rowspan")) {
                    rowspanCount = Integer.parseInt(cell.attr("rowspan"));
                    cell.removeAttr("rowspan");

                    Element copyRow = row;

                    for (int i = rowspanCount; i > 1; i--) {
                        nextRow = copyRow.nextElementSibling();
                        Element cellCopy = cell.clone();
                        Element childTd = nextRow.child(cellIndex);
                        childTd.after(cellCopy);
                    }
                }
            }
        }
}

它将rowspan单元格复制到应包含它的以下所有行。还将删除属性rowspan以删除任何进一步的差异。

我将从上一个小时开始尝试。。你能帮我解答一下吗?我已经用我尝试过的代码编辑了这个问题。getIndex在Jsoup中不可用。看看我的解决方案,更简单:)
Elements rows = table.select("tr > td[rowspan=2]");

for (Element row : rows) {
    row.parent().nextElementSibling().append("<td>$50</td>");
}
for (Element row : rows) {
        int cellIndex = -1;
        if(row.select("td").hasAttr("rowspan")){
            for (Element cell : row.select("td")) {
                cellIndex++;
                if (cell.hasAttr("rowspan")) {
                    rowspanCount = Integer.parseInt(cell.attr("rowspan"));
                    cell.removeAttr("rowspan");

                    Element copyRow = row;

                    for (int i = rowspanCount; i > 1; i--) {
                        nextRow = copyRow.nextElementSibling();
                        Element cellCopy = cell.clone();
                        Element childTd = nextRow.child(cellIndex);
                        childTd.after(cellCopy);
                    }
                }
            }
        }
}