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 |
setAttributeNode |
setAttributeNodeNS |
getAttribute (DOM 1) |
getAttributeNS |
getAttributeNode |
getAttributeNodeNS |
hasAttribute (DOM 2) |
hasAttributeNS |
- | - |
removeAttribute (DOM 1) |
removeAttributeNS |
removeAttributeNode |
- |
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
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.