English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Metodo hasAttribute() dell'HTML DOM

Oggetto Elemento HTML DOM

hasAttribute()Il metodo restituisce un valore booleano che indica se l'elemento specificato ha l'attributo specificato.

UsaresetAttribute()Puoi aggiungere nuovi attributi o modificare i valori degli attributi esistenti dell'elemento.

Sintassi:

element.hasAttribute(attrName)
var p = document.getElementsByTagName("P")[0];
p.hasAttribute("style");
Prova a vedere‹/›

Compatibilità del browser

Tutti i browser supportano completamente il metodo hasAttribute():

Metodo
hasAttribute()èèèèè

valore del parametro

parametrodescrizione
attrNameUna stringa che rappresenta il nome dell'attributo

Dettagli tecnici

Valore di ritorno:Un valore booleano che restituisce true se l'elemento ha l'attributo specificato, altrimenti false
Versione DOM:DOM 2° livello

Più esempi

Trova se l'elemento <a> ha l'attributo 'target'. Se è così, cambia il valore dell'attributo 'target' a "_blank":

//Ottieni l'elemento <a> con id = "myLink"
var a = document.getElementById("myLink");
//Se l'elemento <a> ha l'attributo 'target', imposta il valore a "_blank"
if (a.hasAttribute("target")) {   
a.setAttribute("target", "_blank");
}
Prova a vedere‹/›

Riferimenti correlati

Tutorial HTML:Attributi HTML

Riferimento HTML DOM:Metodo getAttribute()

Riferimento HTML DOM:Metodo setAttribute()

Riferimento HTML DOM:Metodo removeAttribute()

Oggetto Elemento HTML DOM