Element.removeAttribute()
Il metodo Element
removeAttribute()
rimuove l'attributo con il nome specificato dall'elemento.
Sintassi
element.removeAttribute(attrName);
Parametri
attrName
- Una
DOMString
che specifica il nome dell'attributo da rimuovere dall'elemento. Se l'attributo specificato non esiste,removeAttribute()
restituisce senza generare un errore.
Valore di ritorno
undefined
.
Note: Poiché removeAttribute()
non restituisce un valore, non è possibile concatenare più chiamate contemporaneamente per rimuovere più attributi contemporaneamente.
Note di utilizzo
Dovresti usare removeAttribute()
invece di impostare il valore dell'attributo null
direttamente o usando setAttribute()
. Molti attributi non si comportano come previsto se li imposti a null
.
DOM methods dealing with element's attributes:
Not namespace-aware, most commonly used methods | Namespace-aware variants (DOM Level 2) | DOM Level 1 methods for dealing with Attr nodes directly (seldom used) |
DOM Level 2 namespace-aware methods for dealing with Attr nodes directly (seldom used) |
---|---|---|---|
setAttribute (DOM 1) |
setAttributeNS (en-US) |
setAttributeNode (en-US) |
setAttributeNodeNS (en-US) |
getAttribute (DOM 1) |
getAttributeNS (en-US) |
getAttributeNode (en-US) |
getAttributeNodeNS (en-US) |
hasAttribute (DOM 2) |
hasAttributeNS (en-US) |
- | - |
removeAttribute (DOM 1) |
removeAttributeNS (en-US) |
removeAttributeNode (en-US) |
- |
Esempio
// Prima: <div id="div1" align="left" width="200px">
document.getElementById("div1").removeAttribute("align");
// Dopo: <div id="div1" width="200px">
Specifica
DOM Level 2 Core: removeAttribute (introdotta nel DOM Level 1 Core)
Compatibilità con i browser
BCD tables only load in the browser