DOMImplementation.createDocument()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

DOMImplementation.createDocument()方法创建并返回一个 XMLDocument对象。

语法

doc = document.implementation.createDocument(namespaceURI, qualifiedNameStr, documentType);

参数

namespaceURI

被创建的DOMString 文档的 namespace URI 是 namespace URI ,如果文档不属于任何 namespace URI 就为null.

qualifiedNameStr

DOMString 是否包含要创建文档的限定名称,即可选的前缀和冒号,以及本地的根元素。

documentType 可选

文档的 DocumentType 默认为 null.

例子

js
var doc = document.implementation.createDocument(
  "http://www.w3.org/1999/xhtml",
  "html",
  null,
);
var body = document.createElementNS("http://www.w3.org/1999/xhtml", "body");
body.setAttribute("id", "abc");
doc.documentElement.appendChild(body);
alert(doc.getElementById("abc")); // [object HTMLBodyElement]

规范

Specification
DOM
# ref-for-dom-domimplementation-createdocument②

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
createDocument

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见