jQuery检查属性是否包含substring

前端之家收集整理的这篇文章主要介绍了jQuery检查属性是否包含substring前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的问题:考虑以下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">

如何检查$(this)是否包含带有子串“yellow”的类名,例如使用jQuery?

$("div").click(function() {

    if ($(this).contains_the_class_with_the_substring_yellow?) {
        // do something
    }
}

解决方法

$("div").click(function() {

    if (this.className.indexOf("yellow") > -1) {
        // do something
    }
}

猜你在找的jQuery相关文章