2010-10-12 28 views
8

alt dize içeren eğer benim sorunum: $ (this) jQuery kullanarak örneğin "sarı" alt dize ile bir sınıf adı içeriyorsa nasıl kontrol edebilirimjQuery çek niteliği İşte

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

: Aşağıdaki html düşünün ?

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

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

cevap

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

    if (this.className.indexOf("yellow") > -1) { 
     // do something 
    } 
} 
8
$("div").click(function() { 

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

veya saf jQuery'ish: hızlıydı

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

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

, teşekkürler – Tom