XML DOM cloneNode() method

Εγχειρίδιο αναφοράς αντικειμένων Node

Definition and Usage

The cloneNode() method can create an exact copy of the specified node.

This method can return the copied node.

Syntax:

nodeObject.cloneNode(include_all)
Parameter Description
include_all Required. If the logical parameter is set to true, the cloned node will clone all child nodes of the original node.

Return value

Copy of the current node.

Description

This method will copy and return a copy of the node it calls. If a parameter is passed to it as true, it will also recursively copy all descendant nodes of the current node. Otherwise, it will only copy the current node.

The returned node does not belong to the document tree, and its parentNode property is null.

When copying an Element node, all its attributes will be copied. However, it should be noted that the event listener functions registered on the current node will not be copied.

παράδειγμα

Σε όλους τους παραδείγματα, θα χρησιμοποιούμε το αρχείο XML books.xmlκαι οι συνάρτησεις JavaScript loadXMLDoc().

Η παρακάτω κείμενη μονογραφία μπορεί να клωνοποιήσει τον πρώτο κόμβο <book> και να τον προσθέσει στο τέλος της λίστας κόμβων:

xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName('book')[0];
var cloneNode=x.cloneNode(true);
xmlDoc.documentElement.appendChild(cloneNode);
//Έξοδος όλων των τίτλων
var y=xmlDoc.getElementsByTagName("title");
for (i=0;i<y.length;i++)
  {
  document.write(y[i].childNodes[0].nodeValue);
  document.write("<br />");
  }

Έξοδος:

Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML
Everyday Italian

Εγχειρίδιο αναφοράς αντικειμένων Node