2010-10-12 59 views
8

這裏是我的問題:考慮下面的html:jQuery的檢查,如果屬性包含子串

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

如何檢查是否$(本)包含一個類名與子「黃」,例如使用jQuery ?

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

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

回答

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

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

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

或純jQuery'ish:

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

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

那是快,謝謝 – Tom 2010-10-12 13:03:21

相關問題