简体   繁体   中英

How to select a Span from a same-name group based on attribute (e.g. Class)

Suppose my HTML has 2 Spans. They have different IDs but the same Name.

Also, Span 2 has a special Class attribute :

<span id="prime1" name="prime" class="someClass">..</span>
<span id="prime2" name="prime" class="someClass class2">..</span>

In jQuery, how do I see if any Span with name 'Prime' contains the class "class2" ?

I was thinking of a group selector, like this, but not sure how to tweak it:

$('span[name="prime"]:class')

You can use :

if ( $('span[name="prime"]').hasClass('class2').length > 0 ){
   //So there's a span with name 'Prime' contains the class "class2"
}

Or also like @Josh Crozier mentioned in his comment :

if ( $('span.class2[name="prime"]').length > 0 ){
   //So there's a span with name 'Prime' contains the class "class2"
}

hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM