这是我的代码
String html = "<font>fsdfs<font>dfsdf</font>dasdasd</font>";
Document doc = Jsoup.parse(html);
Elements elements = doc.select("font");
for(Element element : elements)
{
element.replaceWith(new Element(Tag.valueOf("span"),"").html(element.html()));
}
System.out.println(doc.html());
我想替换字体标记并放置span标记。在此,它将替换第一个字体标记,但不替换第二个标记
答案 0 :(得分:27)
您也可以像这样替换标签:
String html = "<font>fsdfs<font>dfsdf</font>dasdasd</font>";
Document doc = Jsoup.parse(html);
Elements elements = doc.select("font");
// rename all 'font'-tags to 'span'-tags, will also keep attributs etc.
elements.tagName("span");
System.out.println(doc.html());