Select div (or other element) that class contains

2019-04-18 17:11发布

First of all; example of HTML code:

<div class"grap1"> some conetent</div>
<div class"grap2"> some conetent</div>
<div class"grap3"> some conetent</div>
<div class"blabla">some content></div>

And now i want to select divs that class contains "grap" word somewhere is class name (so in this case first three divs). How can i achieve that ?

This is not working: http://api.jquery.com/attribute-contains-word-selector/

5条回答
我欲成王,谁敢阻挡
2楼-- · 2019-04-18 17:29

Depending on your particular use-case, you could try the attribute prefix selector:

var $grapDivs = $('div[class|="grap"]');

It seems from your sample data that "grap" is always prefixing the class, so if that's the case this may be what you want. If "grap" can be anywhere in the class instead of just at the beginning, use *= instead.

查看更多
疯言疯语
3楼-- · 2019-04-18 17:36

Use the "attribute contains" selector: http://api.jquery.com/attribute-contains-selector/
The "attribute contains word" selector won't work because, according to the docs, it finds elements where the specified attribute contains the given word delimited by spaces.

查看更多
▲ chillily
4楼-- · 2019-04-18 17:41

You want to use the "Attribute contains selector", not the "Attribute contains WORD selector", which is what you linked to.

查看更多
时光不老,我们不散
5楼-- · 2019-04-18 17:46

What you're looking for is the "attribute contains selector"

See an example here http://jsfiddle.net/CsvUY/

查看更多
甜甜的少女心
6楼-- · 2019-04-18 17:50

Use the attribute contains selector:

$('div[class*="grap"]')
查看更多
登录 后发表回答