如何根据属性(例如Class)从同名组中选择Span

时间:2021-12-29 00:02:28

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

假设我的HTML有2个Spans。它们具有不同的ID但名称相同。

Also, Span 2 has a special Class attribute :

另外,Span 2有一个特殊的Class属性:

<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"?

在jQuery中,如何查看名为“Prime”的Span是否包含“class2”类?

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

我在想一个像这样的组选择器,但不知道如何调整它:

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

1 个解决方案

#1


1  

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 :

或者也像@Josh Crozier在评论中提到的那样:

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

hope this helps.

希望这可以帮助。

#1


1  

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 :

或者也像@Josh Crozier在评论中提到的那样:

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

hope this helps.

希望这可以帮助。