JQuery hasAttr () -ekvivalentti - CSS-temppuja

Anonim

jQueryllä ei todellakaan ole .hasAttr()toimintoa. Voit olettaa, että kyllä, mutta valitettavasti ei.

StackOverflow-säikeellä on melko hyviä ratkaisuja.

Hanki attribuutti, tarkista arvo

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 )

Alkuperäisellä JavaScriptillä on tapa

Jos sinulla on vain jQuery-viite ...

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

Suodata valinta

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