2013-11-24 50 views
1

這裏是我的代碼:元素ID

Element current = doc.select("tr[class=row]").get(5); 
    for (Element td : current.children()) { 
      System.out.println(td.text()); 
    } 

我怎樣才能在迴路中的元素ID?

謝謝!

回答

2

在HTML id是一個正常的屬性,所以你可以簡單地調用td.attr("id")

Element current = doc.select("tr.row").get(5); 
for (Element td : current.children()) { 
    System.out.println(td.attr("id")); 
} 

注意,也有對類選擇:tr.row

JSoup支持很多的CSS選擇器,所以這可能是與單個選擇被改寫:

Elements elements = doc.select("tr.row:nth-of-type(6) > td"); 

for (Element element : elements) { 
    System.out.println(element.id()); 
}