2012-07-01 6 views
7

クリックされたli要素に写真のクラスがない場合、コードを実行するif文を作成しようとしています。クリックされた要素に特定のクラスがないか確認してください

$('div.menu li').click(function(){ 
    var content = $(this).attr('class'); 
    if (/* I can't figure out the correct syntax that goes here? */){ 
     $("div.content_wrapper, div.content").hide(); 
     $('div.content_wrapper').animate({width: 'toggle'}); 
     $('div.content_wrapper, div.' + content).show(); 
     $('div.menu li').removeClass('menuactive'); 
     $(this).addClass('menuactive'); 
    } 
}); 

答えて

8

使用:not selector

$("div.menu li:not(.photo)").click(function(){ 
    $("div.content_wrapper, div.content").hide(); 
    $("div.content_wrapper").animate({width: "toggle"}); 
    $("div.content_wrapper, div." + content).show(); 
    $("div.menu li").removeClass("menuactive"); 
    $(this).addClass("menuactive"); 
}); 
+0

+1スマートな答え。 – undefined

+0

面白い、ありがとう! – farrkling

6

ただ使うjQueryの.is

if(!$(this).is('.some-class')) { // ... 

それとも、あなたがそう好む場合、.hasClass

if(!$(this).hasClass('some-class')) { // ... 
+0

素晴らしい!ありがとうございました! – farrkling

+2

代わりに@ EH_warchの答えを使用して、私は外側のセレクタに注意を払っていません:) – Ryan

1
if(!$(this).hasClass('className')) { 
    // clicked element does not have the class 
} 
関連する問題