Range:createContextualFragment() 方法
Range.createContextualFragment() 方法透過呼叫 HTML 片段解析演算法或 XML 片段解析演算法,以範圍的開始(選中節點的父節點)作為上下文節點,來返回一個 DocumentFragment。如果範圍屬於一個 HTMLness 位已設定的 Document,則使用 HTML 片段解析演算法。在 HTML 的情況下,如果上下文節點是 html,出於歷史原因,則使用 body 作為上下文來呼叫片段解析演算法。
語法
js
createContextualFragment(tagString)
引數
-
包含要轉換為文件片段的文字和標籤的文字。
返回值
一個 DocumentFragment 物件。
示例
js
const tagString = "<div>I am a div node</div>";
const range = document.createRange();
// Make the parent of the first div in the document become the context node
range.selectNode(document.getElementsByTagName("div").item(0));
const documentFragment = range.createContextualFragment(tagString);
document.body.appendChild(documentFragment);
規範
| 規範 |
|---|
| HTML # dom-range-createcontextualfragment |
瀏覽器相容性
載入中…