Un jQuery hasAttr () Equivalente - Trucos CSS

Tabla de contenido

jQuery realmente no tiene una .hasAttr()función. Puede suponer que lo hace, pero lamentablemente no es así.

Un hilo StackOverflow tiene algunas soluciones bastante buenas.

Obtenga el atributo, verifique el valor

var attr = $(this).attr('name'); // For some browsers, `attr` is undefined; for others, `attr` is false. Check for both. if (typeof attr !== typeof undefined && attr !== false) ( // Element has this attribute )

JavaScript nativo tiene una forma

Si solo tiene una referencia de jQuery ...

$(this)(0).hasAttribute("name"); jQObject(0).hasAttribute("name");

Filtrar la selección

$(this).is('(name)'); $(this).filter("(name='choice')");

Articulos interesantes...