CharacterData:replaceWith() 方法
CharacterData 接口的 replaceWith() 方法用一组 Node 对象或字符串替换此节点在其父节点的子节点列表中的位置。
语法
js
replaceWith(...nodes)
参数
注意: 如果不传递任何参数,此方法将从 DOM 树中移除该节点。
返回值
无(undefined)。
异常
HierarchyRequestErrorDOMException-
在节点无法插入到层次结构中的指定位置时抛出。
示例
html
<p id="myText">Some text</p>
js
let text = document.getElementById("myText").firstChild;
let em = document.createElement("em");
em.textContent = "Italic text";
text.replaceWith(em); // Replace `Some text` by `Italic text`
规范
| 规范 |
|---|
| DOM # ref-for-dom-childnode-replacewith① |
浏览器兼容性
加载中…