我有以下的HTML如何替換元素?
<html>
<head>
<title>test</title>
</head>
<body>
<table>
<caption>table title and/or explanatory text</caption>
<thead>
<tr>
<th>header</th>
</tr>
</thead>
<tbody>
<tr>
<td id=\"test\" width=\"272\"></td>
</tr>
</tbody>
</table>
<a href=\"http://www.google.fi\" style=\"color:black\">Test link</a>
<a href=\"http://www.google.fi\"><img src=\"http://www.google.se/images/nav_logo95.png\" /></a>"
</body>
</html>;
而且我想找到jsoup第一環節,並用文字代替它
Element elem = page.select("a[href=" + link.getUrl() + "]:contains(" + link.getName() + ")").first();
我只能elem.html("foo")
替換內部HTML或打印outerHtml與elem.outerHtml()
有誰知道我該如何做到這一點?
我所做的事情不對,就是得到了錯誤的元素。到目前爲止,我發現選擇正確的元素的最佳方式是遍歷所有元素。如果還沒有找到確切的循環,嵌套循環會有所幫助。例如: for(Element elem:contElems){if(elem.id()!=「」)System.out.println(elem.id()); \t 元素contElems1 = contElems.select(「href,a [href],a [data-href],link [href],a [title]」); 爲(元素elem1:contElems1){// 把代碼在這裏 } // (或/和)把代碼在這裏 } –
我沒有去了解如何把一個換行符在評論上述... –