How to form a Query Select That Works With Ands an

2019-07-29 08:05发布

问题:

I have a class defined HtmlElements as follows:

<div class="GM BOLTA 2016" >
<div class="GM BOLTB 2016" >
<div class="GM BOLT 2015" >
<div class="FORD BOLT 2016" >

I want to be able to make query selectors against this and specifically, I'd like to know all div tags that have GM and also have anything that starts with BOLT and is not 2015.

I'm thinking something like

[GM][^BOLT]!2015 

but obviously wrong.

回答1:

I'd like to know all div tags that have GM and also have anything that starts with BOLT and is not 2015.

Plain Javascript:

var matches = document.querySelectorAll('.GM, [class^="BOLT"]:not(.2015)');

jQuery:

$('.GM, [class^="BOLT"]:not(.2015)')