# 設置一個元素的HTML內容
<div>
<h1><span style="font-size: 14px; line-height: 1.5;"></span></h1></div><b style="background-color: rgb(255, 255, 255); font-size: 21px;">問題</b><div><h1><span style="font-size: 14px; line-height: 1.5;"></span></h1>
<p>你需要一個元素中的HTML內容</p>
<h2>方法</h2>
<p>可以使用<code><a title="A HTML element consists of a tag name, attributes, and child nodes (including text nodes and other elements)." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html">Element</a></code>中的HTML設置方法具體如下:</p>
<pre><code>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>
</code></pre>
<h2>說明</h2>
<ul>
<li><code><a title="Set this element's inner HTML." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#html%28java.lang.String%29">Element.html(String
html)</a></code> 這個方法將先清除元素中的HTML內容,然后用傳入的HTML代替。</li>
<li><code><a title="Add inner HTML into this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#prepend%28java.lang.String%29">Element.prepend(String
first)</a></code> 和 <code><a title="Add inner HTML to this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#append%28java.lang.String%29">Element.append(String
last)</a></code> 方法用于在分別在元素內部HTML的前面和后面添加HTML內容</li>
<li><code><a title="Wrap the supplied HTML around this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#wrap%28java.lang.String%29">Element.wrap(String
around)</a></code> 對元素包裹一個外部HTML內容。</li>
</ul>
<h2>參見</h2>
<p>可以查看API參考文檔中 <code><a title="Create a new element by tag name, and add it as the first child." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#prependElement%28java.lang.String%29">Element.prependElement(String
tag)</a></code>和<code><a title="Create a new element by tag name, and add it as the last child." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#appendElement%28java.lang.String%29">Element.appendElement(String
tag)</a></code> 方法來創建新的元素并作為文檔的子元素插入其中。</p></div>
- Introduction
- 爬蟲相關技能介紹
- 爬蟲簡單介紹
- 爬蟲涉及到的知識點
- 爬蟲用途
- 爬蟲流程介紹
- 需求描述
- Http請求處理
- http基礎知識介紹
- http狀態碼
- httpheader
- java原生態處理http
- URL類
- 獲取URL請求狀態
- 模擬Http請求
- apache httpclient
- Httpclient1
- httpclient2
- httpclient3
- httpclient4
- httpclient5
- httpclient6
- okhttp
- OKhttp使用教程
- 技術使用
- java執行javascript
- 網頁解析
- Xpath介紹
- HtmlCleaner
- HtmlCleaner介紹
- HtmlCleaner使用
- HtmlParser
- HtmlParser介紹
- Jsoup
- 解析和遍歷一個HTML文檔
- 解析一個HTML字符串
- 解析一個body片斷
- 從一個URL加載一個Document
- 從一個文件加載一個文檔
- 使用DOM方法來遍歷一個文檔
- 使用選擇器語法來查找元素
- 從元素抽取屬性,文本和HTML
- 處理URLs
- 示例程序 獲取所有鏈接
- 設置屬性的值
- 設置一個元素的HTML內容
- 消除不受信任的HTML (來防止XSS攻擊)
- 正則表達式
- elasticsearch筆記
- 下載安裝elasticsearch
- 檢查es服務健康