DOMImplementation对象createDocument()方法

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

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

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

参数

  • namespaceURI是要创建的文档元素的名称空间URI或null
  • qualifiedName是要创建的文档元素的限定名称或null
  • doctype是要创建的文档类型或null
  • 此方法返回一个带有document元素的新Document对象。

示例

下面的示例演示了createDocument()方法的用法 -

<!DOCTYPE html>
<html>
   <meta charset="utf-8"/>
    <head>

   </head>
   <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对象 下一篇: DOM DocumentType对象