Range:surroundContents() 方法
surroundContents() 方法是 Range 接口的一部分,它使用提供的节点来包裹选中的内容。该方法会 提取 选区的内容,用提取的内容替换 newParent 的子节点,将 newParent 插入 到提取内容的原位置,并使选区选中 newParent。
如果选区部分包含任何非 Text 节点,则会抛出异常。选区只能包含文本节点和完全选中的节点。
语法
js
surroundContents(newParent)
参数
返回值
无(undefined)。
示例
HTML
html
<span class="header-text">Put this in a headline</span>
JavaScript
js
const range = document.createRange();
const newParent = document.createElement("h1");
range.selectNode(document.querySelector(".header-text"));
range.surroundContents(newParent);
结果
规范
| 规范 |
|---|
| DOM # dom-range-surroundcontents |
浏览器兼容性
加载中…