DOM - DOMImplementation 物件方法 - createdocument



createDocument() 方法用於建立一個指定型別的 DOM 文件物件及其文件元素。

語法

以下是 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.htm(此檔案和 node.xml 應位於伺服器上的同一路徑)。我們將得到如下所示的輸出:

[object HTMLBodyElement]
dom_domimplementation_object.htm
廣告
© . All rights reserved.