jQuery 例如,选择类包含“grap”(o 其他指定词)的 div(或其他元素)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7378382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:45:29  来源:igfitidea点击:

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

jqueryclassattributeswordcontains

提问by born2fr4g

First of all; example of HTML code:

首先; HTML代码示例:

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

现在我想选择类包含“grap”词的 div 是类名(所以在这种情况下是前三个 div)。我怎样才能做到这一点?

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

这不起作用:http: //api.jquery.com/attribute-contains-word-selector/

回答by Andrei

Use the attribute contains selector:

使用包含选择器属性

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

回答by FtDRbwLXw6

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.

从您的示例数据看来,“grap”始终是类的前缀,因此如果是这种情况,这可能就是您想要的。如果“grap”可以在类中的任何位置而不是仅在开头,请使用 *= 代替。

回答by Abraham

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.

使用“属性包含”选择器:http: //api.jquery.com/attribute-contains-selector/
“属性包含词”选择器不起作用,因为根据文档,它会找到指定属性包含的元素由空格分隔的给定单词。

回答by maxedison

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

您想使用“属性包含选择器”,而不是“属性包含 WORD 选择器”,这是您链接到的。

回答by Kemal Fadillah

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

您正在寻找的是“属性包含选择器”

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

在此处查看示例http://jsfiddle.net/CsvUY/