Un jQuery hasAttr () Equivalent - Trucchi CSS

Anonim

jQuery non ha davvero una .hasAttr()funzione. Potresti presumere che lo sia, ma purtroppo non è così.

Un thread StackOverflow ha alcune soluzioni piuttosto buone.

Ottieni l'attributo, controlla il valore

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 ha un modo

Se hai solo un riferimento jQuery ...

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

Filtra la selezione

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