We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

这篇翻译不完整。请帮忙从英语翻译这篇文章

Range.surroundContents()方法将Range对象的内容移动到一个新的节点, 并将这个新节点放到指定区域.

这个方法与newNode.appendChild(range.extractContents()); range.insertNode(newNode)很相近. 选定后, range拥有了节点标签.

也有例外情况, 如果选定的Range区域包含仅有一个节点标签的Text. 那标签将不会自动成对生成,操作将失败.

语法

range.surroundContents(newNode);

参数

newNode
一个放置区域内容的目标Node .

例子

var range = document.createRange();
var newNode = document.createElement("p");

range.selectNode(document.getElementsByTagName("div").item(0));
range.surroundContents(newNode);

规范

Specification Status Comment
DOM
Range.surroundContents()
Living Standard No change.
Document Object Model (DOM) Level 2 Traversal and Range Specification
Range.surroundContents()
Obsolete Initial specification.

浏览器兼容

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) 1.0 (1.7 or earlier) [1] 9.0 9.0 (Yes)
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) 1.0 (1.0) [1] 9.0 9.0 (Yes)

[1] Before Firefox 1.5, a bug was preventing Range.surroundContents to work if the start and end of range were in same textnode (bug 135928).

相关链接

文档标签和贡献者

此页面的贡献者: teoli, chyancheng
最后编辑者: chyancheng,