Home >Web Front-end >HTML Tutorial >[Jsoup Learning Etiquette] Set the HTML content of an element_html/css_WEB-ITnose
You need the HTML content in an element
You can use the HTML setting method in Element as follows:
Element div = doc.select("div").first(); // <div></div>div.html("<p>lorem ipsum</p>"); // <div><p>lorem ipsum</p></div>div.prepend("<p>First</p>");//在div前添加html内容div.append("<p>Last</p>");//在div之后添加html内容// 添完后的结果: <div><p>First</p><p>lorem ipsum</p><p>Last</p></div>Element span = doc.select("span").first(); // <span>One</span>span.wrap("<li><a href='http://example.com/'></a></li>");// 添完后的结果: <li><a href="http://example.com"><span>One</span></a></li>
You can view the Element.prependElement(String tag) and Element.appendElement(String tag) methods in the API reference document to create new elements and insert them as child elements of the document.