DOM - DOMImplementation 对象方法 - createdocument

方法 createDocument () 用于创建具有其文档元素的指定类型的 DOM Document 对象。

语法

以下是 createDocument () 方法的语法。

Document doc = document.implementation.createDocument
    (namespaceURI,qualifiedNameStr,documentType);
  • namespaceURI 是要创建的文档元素的命名空间 URI,或者为 null。

  • qualifiedName 是要创建的文档元素的限定名称,或者为 null。

  • doctype 是要创建的文档的类型,或者为 null。

  • 此方法返回一个带有其文档元素的新 Document 对象。

示例

以下示例演示了 createDocument () 方法的用法 −

<!DOCTYPE html>
<html>
   <body>
      <script>
         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', 'Company');
         doc.documentElement.appendChild(body);
         document.write(doc.getElementById('Company')); // [object HTMLBodyElement]
      </script>
   </body>
</html>

执行

将此文件另存为 domimplementation_createdocument.html 并保存在服务器路径上(此文件和 node.xml 应位于服务器中的同一路径上)。我们将获得如下所示的输出 −

[object HTMLBodyElement]
dom_domimplementation_object.htm