2012-07-01 25 views
7

Tôi đang cố gắng tạo câu lệnh if chạy mã nếu phần tử li được nhấp không có lớp ảnh.Kiểm tra xem phần tử đã nhấp không có lớp cụ thể

$('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'); 
    } 
}); 

Trả lời

8

Sử dụng :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 câu trả lời thông minh. – undefined

+0

Thú vị, cảm ơn bạn! – farrkling

6

Chỉ cần sử dụng jQuery .is:

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

Hoặc, nếu bạn rất thích, .hasClass:

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

Tuyệt vời! Cảm ơn bạn! – farrkling

+2

Sử dụng câu trả lời của @ EH_warch thay vào đó, tôi không chú ý đến bộ chọn ngoài :) – Ryan

1
if(!$(this).hasClass('className')) { 
    // clicked element does not have the class 
} 
Các vấn đề liên quan