English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
element.hasAttribute(attrName)
var p = document.getElementsByTagName("P")[0]; p.hasAttribute("style");Prova a vedere‹/›
Tutti i browser supportano completamente il metodo hasAttribute():
Metodo | |||||
hasAttribute() | è | è | è | è | è |
parametro | descrizione |
---|---|
attrName | Una stringa che rappresenta il nome dell'attributo |
Valore di ritorno: | Un valore booleano che restituisce true se l'elemento ha l'attributo specificato, altrimenti false |
---|---|
Versione DOM: | DOM 2° livello |
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‹/›
Tutorial HTML:Attributi HTML
Riferimento HTML DOM:Metodo getAttribute()
Riferimento HTML DOM:Metodo setAttribute()
Riferimento HTML DOM:Metodo removeAttribute()