DocumentType:replaceWith() 方法

Baseline 已广泛支持

此功能已成熟,可跨多种设备和浏览器版本工作。它自 ⁨2018 年 4 月⁩ 起已在所有浏览器中可用。

DocumentType.replaceWith() 方法使用给定的一组节点来替换文档类型。

语法

js
replaceWith(node1)
replaceWith(node1, node2)
replaceWith(node1, node2, /* …, */ nodeN)

参数

node1, …, nodeN

用于替换 DocumentType 的一组节点。

返回值

无(undefined)。

异常

HierarchyRequestError DOMException

在节点无法插入到层次结构中的指定位置时抛出。

示例

使用 replaceWith()

js
let svg_dt = document.implementation.createDocumentType(
  "svg:svg",
  "-//W3C//DTD SVG 1.1//EN",
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd",
);

document.doctype.replaceWith(svg_dt);

规范

规范
DOM
# ref-for-dom-childnode-replacewith①

浏览器兼容性

另见