XML DOM appendChild()Método


Element 对象参考手册Objeto Element

Definición y uso

El método appendChild() agrega un nodo después del último nodo hijo del elemento especificado.

Este método devuelve el nuevo nodo hijo.

Sintaxis

appendChild(node)

Parámetros Descripción
node Obligatorio. El nodo a agregar.


Ejemplo 1

El siguiente fragmento de código utilizaloadXMLDoc()para cargar "" en xmlDoc, crea un nodo (<edition>) y lo agrega después del último nodo hijo del primer elemento <book>:

Ejemplo

xmlDoc=loadXMLDoc("books.xml");

newel=xmlDoc.createElement("edition");

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);

document.write(x.getElementsByTagName("edition")[0].nodeName);

Salida:

edition

Pruébalo »

Ejemplo 2

El siguiente fragmento de código utilizaloadXMLDoc()para cargar "" en xmlDoc y agrega un nuevo nodo a todos los elementos <book>:

Ejemplo

xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book"); for (i=0;i<x.length;i++) { newel=xmlDoc.createElement("edition"); newtext=xmlDoc.createTextNode("first"); newel.appendChild(newtext); x[i].appendChild(newel); } //Muestra todos los title y edition y=xmlDoc.getElementsByTagName("title"); z=xmlDoc.getElementsByTagName("edition"); for (i=0;i<y.length;i++) { document.write(y[i].childNodes[0].nodeValue); document.write(" - Edition: "); document.write(z[i].childNodes[0].nodeValue); document.write("<br>"); }

Salida:

Everyday Italian - Edition: First
Harry Potter - Edition: First
XQuery Kick Start - Edition: First
Learning XML - Edition: First

Pruébalo »

Element 对象参考手册Objeto Element Otras extensiones