XML DOM setAttribute()Método
Objeto Element
Definición y uso
El método setAttribute() agrega un nuevo atributo.
Si ya existe un atributo con el nombre especificado en el elemento, su valor se cambia al valor del parámetro value.
Sintaxis
elementNode.setAttribute(name,value)
| Parámetros | Descripción |
|---|---|
| name | Requerido. Especifica el nombre del atributo a establecer. |
| value | Requerido. Especifica el valor del atributo a establecer. |
Ejemplo
El siguiente fragmento de código usaloadXMLDoc()carga "en xmlDoc, y agrega el atributo "edition" a todos los elementos <book>:
Ejemplo
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('title');
//Agrega un nuevo atributo a cada elemento title
for(i=0;i<x.length;i++)
{
x[i].setAttribute("edition","first");
}
//Muestra los valores de title y edition
for (i=0;i<x.length;i++)
{
document.write(x[i].childNodes[0].nodeValue);
document.write(" - Edition: ");
document.write(x[i].getAttribute('edition'));
document.write("<br>");
}
Salida:
Everyday Italian - Edition: FIRST
Harry Potter - Edition: FIRST
XQuery Kick Start - Edition: FIRST
Learning XML - Edition: FIRST
Harry Potter - Edition: FIRST
XQuery Kick Start - Edition: FIRST
Learning XML - Edition: FIRST
Pruébalo »
Pruébalo
Objeto Element Otras extensiones