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①

瀏覽器相容性

另見