Sei così vicino è pazzesco.
Non c'è hasAttr ma il colpire un attributo in base al nome restituirà undefined se non esiste.
Ecco perché funziona di seguito. Se rimuovi l'attributo nome da #heading, il secondo avviso verrà attivato.
Aggiornamento: Come per i commenti, il seguito sarà SOLO lavoro se l'attributo è presente E è impostato su qualcosa che non se l'attributo è lì, ma vuota
<script type="text/javascript">
$(document).ready(function()
{
if ($("#heading").attr("name"))
alert('Look, this is showing because it\'s not undefined');
else
alert('This would be called if it were undefined or is there but empty');
});
</script>
<h1 id="heading" name="bob">Welcome!</h1>
fonte
2009-08-23 08:42:15
qui è un plugin jQuery che sembra fare esattamente quello che stai cercando -> http://plugins.jquery.com/project/hasAttr – bmarti44
bmarti44 Sembra che il link marcito alcuni. – MrBoJangles
http://stackoverflow.com/questions/1318076/jquery-hasattr-checking-to-see-if-the-is-an-attribute-on-an-element qui è la risposta migliore. – Imad