Home  >  Article  >  Web Front-end  >  【jsoup的学习礼记】设置一个元素的HTML内容_html/css_WEB-ITnose

【jsoup的学习礼记】设置一个元素的HTML内容_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:48:26969browse

问题

你需要一个元素中的HTML内容

方法

可以使用Element中的HTML设置方法具体如下:

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("
  • ");// 添完后的结果:
  • One
  • 说明

  • Element.html(String html) 这个方法将先清除元素中的HTML内容,然后用传入的HTML代替。
  • Element.prepend(String first) 和 Element.append(String last) 方法用于在分别在元素内部HTML的前面和后面添加HTML内容
  • Element.wrap(String around) 对元素包裹一个外部HTML内容。
  • 参见

    可以查看API参考文档中 Element.prependElement(String tag)和Element.appendElement(String tag) 方法来创建新的元素并作为文档的子元素插入其中。

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn