Esempio
Impostare la class nodo attributo di un <h1> Elemento:
var h1 = document.getElementsByTagName("H1")[0]; // Get the
first <h1> element in the document
var att =
document.createAttribute("class"); //
Create a "class" attribute
att.value = "democlass";
// Set the value of the class attribute
h1.setAttributeNode(att);
// Add the class attribute to <h1>
Prima di impostare il nodo attributo:
Hello World
Dopo aver impostato il nodo attributo:
Hello World
Prova tu stesso " Più "Try it Yourself" esempi di seguito.
Definizione e l'utilizzo
Il setAttributeNode() metodo aggiunge il specified nodo attributo a un elemento.
Se la specified attributo esiste già, questo metodo sostituisce.
Il valore di ritorno di questo metodo è un oggetto Attr. Per ulteriori informazioni, vedere L'Oggetto HTML DOM Attribute .
Si veda anche il setAttribute() metodo.
Tip: Utilizzare la removeAttributeNode() metodo per rimuovere un nodo di attributi da un elemento.
Supporto browser
Metodo | |||||
---|---|---|---|---|---|
setAttributeNode() | sì | sì | sì | sì | sì |
Sintassi
element .setAttributeNode( attributenode )
valori dei parametri
Parametro | Tipo | Descrizione |
---|---|---|
attributenode | Attr object | Necessario. Il nodo attributo che si desidera aggiungere |
Dettagli tecnici
Valore di ritorno: | Un oggetto Attr, che rappresenta l' replaced nodo attributo, se presenti, altrimenti nullo |
---|---|
DOM Version | Nucleo Livello 1 elemento OBJECT |

Altri esempi
Esempio
Impostare l' href nodo attributo di un <a> elemento:
var anchor = document.getElementById("myAnchor"); // Get the <a>
element with id="myAnchor"
var att = document.createAttribute("href");
// Create a "href" attribute
att.value = "http://www.w3ii.com";
// Set the value of the href attribute
anchor.setAttributeNode(att);
// Add the href attribute to <a>
Prima di impostare il nodo attributo:
Go to w3ii.com
Dopo aver impostato il nodo attributo:
Prova tu stesso "Pagine correlate
HTML Tutorial: attributi HTML
HTML DOM Riferimento: Il HTML DOM attributo dell'oggetto
HTML DOM Riferimento: href="met_element_setattribute.html"> setAttribute() Method
HTML DOM Riferimento: href="met_document_createattribute.html">document. createAttribute() Method href="met_document_createattribute.html">document. createAttribute() Method
HTML DOM Riferimento: attributo .value proprietà
HTML DOM Riferimento: href="met_element_getattributenode.html"> getAttributeNode() Method
HTML DOM Riferimento: href="met_element_removeattributenode.html"> removeAttributeNode() Method