2012-07-01 64 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 
} 
相关问题