jQuery 检查属性是否包含子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3914900/
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 16:20:44 来源:igfitidea点击:
jQuery check if attribute contain substring
提问by Tom
Here's my problem: Consider the following html:
这是我的问题:考虑以下 html:
<div id="item1" class="green_large">
<div id="item2" class="green_large">
<div id="item2" class="green_small">
<div id="item4" class="yellow_large">
<div id="item5" class="yellow_large">
How do I check if $(this)contain a class name with the substring "yellow" for example using jQuery?
例如,如何使用 jQuery检查$(this) 是否包含带有子字符串“yellow”的类名?
$("div").click(function() {
if ($(this).contains_the_class_with_the_substring_yellow?) {
// do something
}
}
回答by Mikee
$("div").click(function() {
if (this.className.indexOf("yellow") > -1) {
// do something
}
}
回答by jAndy
$("div").click(function() {
if (this.className.indexOf('yellow') > -1) {
// do something
}
}
or pure jQuery'ish:
或纯jQuery'ish:
$("div").click(function() {
if ($(this).attr('class').indexOf('yellow') > -1) {
// do something
}
}