定義:wrap() 方法為被選元素添加父元素;
unwrap() 方法删除被選元素。
public static void main(String[] args) {
//img标簽添加fingure父标簽,如果父标簽非p标簽進行替換
String html = "<p><img src=\"https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png\"></p>";
Document doc = Jsoup.parse(html);
Elements imgs = doc.getElementsByTag("img");
for (Element image : imgs) {
if ("p".equals(image.parent().tagName())) {
image.parent().unwrap();
}
image.wrap("<figure class=\"image\">");
}
System.out.println(doc.toString());
}