开发者

jQuery check if attribute contain substring

开发者 https://www.devze.com 2023-01-19 12:19 出处:网络
Here\'s my problem: Consider the following html: <div id=\"item1\" class=\"green_large\"> <div id=\"item2\" class=\"green_large\">

Here's my problem: Consider the following 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?

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

    if ($(this).contains_the_class_with_the_substri开发者_JS百科ng_yellow?) {
        // do something
    }
}


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

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


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

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

or pure jQuery'ish:

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

    if ($(this).attr('class').indexOf('yellow') > -1) {
        // do something
    }
}
0

精彩评论

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

关注公众号