Range.surroundContents() method moves content of the 范围 into a new node, placing the new node at the start of the specified range.

This method is nearly equivalent to newNode.appendChild( range.extractContents() ); range.insertNode (newNode) . After surrounding, the boundary points of the range 包括 newNode .

An exception will be thrown, however, if the 范围 splits a non- 文本 node with only one of its boundary points. That is, unlike the alternative above, if there are partially selected nodes, they will not be cloned and instead the operation will fail.

句法

range.surroundContents(newParent);
					

参数

newParent
节点 with which to surround the contents.

范例

HTML

<span class="header-text">Put this in a headline</span>
					

JavaScript

const range = document.createRange();
const newParent = document.createElement('h1');
range.selectNode(document.querySelector('.header-text'));
range.surroundContents(newParent);
					

结果

规范

规范 状态 注释
DOM
The definition of 'Range.surroundContents()' in that specification.
实时标准 无变化。
Document Object Model (DOM) Level 2 Traversal and Range Specification
The definition of 'Range.surroundContents()' 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
surroundContents Chrome Yes Edge 12 Firefox 4 IE 9 Opera 9 Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: