XML DOM setAttribute método ()
Definición y Uso
setAttribute método () para añadir una nueva propiedad.
Si el atributo especifica el nombre ya existe, su valor se cambia al parámetro de valor.
gramática
elementNode.setAttribute(name,value)
参数 | 描述 |
---|---|
name | 必需。规定要设置的属性的名称。 |
value | 必需。规定要设置的属性的值。 |
Ejemplos
El fragmento de código siguiente utiliza loadXMLDoc () a " la Books.XML " en xmlDoc y añadir "edición" atributo a todo elemento <libro>:
Ejemplos
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book");
for(i=0;i<x.length;i++)
{
x.item(i).setAttribute("edition","first");
}
//Output book title and edition value
x=xmlDoc.getElementsByTagName("title");
for (i=0;i<x.length;i++)
{
document.write(x[i].childNodes[0].nodeValue);
document.write(" - Edition: ");
document.write(x[i].parentNode.getAttribute('edition'));
document.write("
");
}
x=xmlDoc.getElementsByTagName("book");
for(i=0;i<x.length;i++)
{
x.item(i).setAttribute("edition","first");
}
//Output book title and edition value
x=xmlDoc.getElementsByTagName("title");
for (i=0;i<x.length;i++)
{
document.write(x[i].childNodes[0].nodeValue);
document.write(" - Edition: ");
document.write(x[i].parentNode.getAttribute('edition'));
document.write("
");
}
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
Trate »
Trate Demos
setAttribute () - cambiar el valor de la propiedad
objeto Element