定义: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());
}