2010-10-12 3 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

Смежные вопросы