Estás tan cerca que es una locura.
if($(this).attr("name"))
No hay hasattr pero lanzó un atributo por su nombre simplemente devolverá indefinido si no existe.
Esta es la razón por la que funciona a continuación. Si elimina el atributo de nombre de # encabezado, se activará la segunda alerta.
Actualización: De acuerdo con los comentarios, el siguiente será SOLO trabajo si el atributo está presente Y se establece en algo que no es si el atributo está ahí, pero vacía
<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>
aquí es un plugin de jQuery que parece hacer exactamente lo que busca -> http://plugins.jquery.com/project/hasAttr – bmarti44
bmarti44 Parece que el enlace se pudría alguna. – MrBoJangles
http://stackoverflow.com/questions/1318076/jquery-hasattr-checking-to-see-if-there-is-an-attribute-on-an-element Aquí hay una mejor respuesta. – Imad