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 |
浏览器兼容性
加载中…