Range.createContextualFragment() 方法返回 DocumentFragment by invoking the HTML fragment parsing algorithm or the XML fragment parsing algorithm with the start of the range (the parent of the selected node) as the context node. The HTML fragment parsing algorithm is used if the range belongs to a Document whose HTMLness bit is set. In the HTML case, if the context node would be html , for historical reasons the fragment parsing algorithm is invoked with body as the context instead.

句法

documentFragment = range.createContextualFragment(tagString)
					

参数

tagString

Text that contains text and tags to be converted to a document fragment.

范例

var tagString = "<div>I am a div node</div>";
var range = document.createRange();
// Make the parent of the first div in the document becomes the context node
range.selectNode(document.getElementsByTagName("div").item(0));
var documentFragment = range.createContextualFragment(tagString);
document.body.appendChild(documentFragment);
					

规范

规范 状态 注释
DOM 剖析和序列化
The definition of 'Range.createContextualFragment()' in that specification.
工作草案 最初的规范。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
createContextualFragment Chrome Yes Edge 12 Firefox Yes IE 11 Opera 15 Safari 9 WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android 14 Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

另请参阅

元数据

  • 最后修改: