开发者

Select div (or other element) that class contains "grap" (o other specified word) for example

开发者 https://www.devze.com 2023-04-04 06:14 出处:网络
First of all; example of HTML code: <div class\"grap1\"> some conetent</div> <div class\"grap2\"&开发者_如何转开发gt; some conetent</div>

First of all; example of HTML code:

<div class"grap1"> some conetent</div>
<div class"grap2"&开发者_如何转开发gt; 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/


Use the attribute contains selector:

$('div[class*="grap"]')


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.


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.


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


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号