English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
importNode()method creates a copy of a node from another document.
If the second parameter is set to true, the child nodes (descendants) of the imported node will also be imported.
The imported node is not yet included in the document tree. To include it, you need to call an insertion method, such aselement.appendChild()orelement.insertBefore()with the nodes currently in the document tree.
withdocument.adoptNode()differently, the original node will not be deleted from its original document.
You can also useelement.cloneNode()The method copies nodes from the current document without deleting them.
document.importNode(externalNode, deep)
var iframe = document.querySelector('iframe'); var iframeImages = iframe.contentDocument.querySelectorAll('h2'); var newParent = document.getElementById('result'); iframeImages.forEach(function(elem) { newParent.appendChild(document.importNode(elem, true)); });Test and see‹/›
All browsers fully support the importNode() method:
Method | |||||
importNode() | is | is | is | is | is |
Parameter | Description |
---|---|
externalNode | The node imported from another document |
deep | A boolean value specifying whether to importexternalNodeThe entire DOM subtree:
|
Return Value: | Represents the Node object of the imported node |
---|---|
DOM Version: | DOM 2Level |