2010-04-20 20 views
2

我有一個左側的導航,顯示/隱藏內容的權利。目前,當您點擊一個鏈接時,它將淡入右側的相應內容,並將活動的類添加到該鏈接。我的問題是,如果再次單擊活動鏈接,右側的內容會再次消失。我想在鏈接處於活動狀態時解除該點擊,並且如果您單擊另一個導航鏈接(隨後從上一個鏈接中刪除該類並將其添加到當前鏈接中),請將點擊事件重新綁定到所有不活動的鏈接。禁用/取消綁定點擊活動項目,重新綁定時點擊無效

這裏是我當前的代碼:

$('.mixesSidebar ul li').click(function() { 
      //Get the attribute id 
      var liId = $(this).attr('id'); 
      //removeClass active from all li's, addClass active to this 
     $('.mixesSidebar ul li').removeClass('active'); 
     $(this).addClass('active'); 
      //Hide all content on the right 
     $('.mixesContent ul').hide(); 
      //Find the content with the same class as the clicked li's ID, and fadeIn 
     $('.mixesContent').find('.' + liId).fadeIn('slow'); 
}); 

非常感謝您的幫助!

回答

1

如果您檢查$(this)是否已經激活,如果沒有,請繼續執行該代碼。

所以,在函數的開始,去:

if(! $(this).hasClass('active')) 
{ 
    // do stuff 
} 
0

如果我正確地理解了這一點,我只是將一個「主動」或某個類添加到已淡入的div中,然後檢查它是否已經具有該類,如果它確實存在,淡化它。

編輯因爲我的描述是明確的泥:

if (!$(this).hasClass('active')) { 
    ... 
} 

很多比解除綁定及重新綁定更穩定:

$('.mixesContent').find('.' + liId).addClass("active_list"); 

if (! $('.mixesContent').find('.' + liId).hasClass("active_list"){ 
    //do the fade 
} 
1

環繞的與邏輯。