简体   繁体   中英

How to select elements using jQuery by partial ID and a class at the same time?

When I want to select all the components by a partial ID or by its class, I use the following.

var identify = $("[id*=donkey]");
var classify = $(".wonkey");

The thing is now that I want to restrict the set returned by identification so that each element still in there also is a member of classification .

The closest I've got by goolearching is along the suggestion here but that's only valid for elements that are children under their parent. That's not my case. I want to select the elements like this.

<bopp id="head-donkey" class="wonkey" />
<bopp id="donkey-butt" class="wonkey" />
<bopp id="head-donkey-butt" class="wonkey" />

But I want to exclude elements like this.

<bopp id="banana" class="wonkey" />
<bopp id="don-key" class="wonkey" />
<bopp id="donkey" class="not-won-key" />

I can't find any info on that operation, though...

You can use the solution linked by you. However, that user has a space between the selectors, which means that you're targeting the descendant using the second selector.

In your specific case, you need to omit space like this.

var idenclassify = $("[id*=donkey].wonkey");

You can use multiple selectors to get the exact elements you need. For your example do:

$(".wonkey[id*=donkey]")

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