2010-08-18 66 views
0

我有一個點擊事件,在某些AJAX內容中動畫顯示到頁面上。事件完成後jquery undelegate點擊事件

一旦這個內容顯示出來,並且用戶點擊了激活該過程的相同鏈接,我現在想要反轉該過程並關閉當前打開的飛出內容。

目前打開的飛出是通過點擊「關閉」鏈接或點擊序列中的另一個飛出鏈接關閉的。如果用戶點擊當前的飛出鏈接,那麼我希望當前的飛行關閉。

// Close fly out function 
function closeFlyout() { 

    $('.fly_container').animate({ 
     'right': '-332' 
    }, 300, 'swing', function() { 
     $(this).detach(); 
     /* TODO: z-index issues in IE7, IE6 
     $('.dark_overlay').fadeOut(300, function() { 
     $(this).remove(); 
     }); 
     */ 
    }); 

}; 

$('.widget').delegate('.widget .fly_out', 'click', function() { 

    /* 
    TODO: z-index issues in IE7, IE6 
    $('body').prepend('<div class="dark_overlay" />'); 
    */ 

    var $widget = $(this).closest('.widget'); 

    var $flyOutIndex = $(this).index('.fly_out'); 

    if ($flyOutIndex == 0) { 
     $flyOutURL = 'Content/HTMLSnippets/Flyouts/Priceassessment/product_order.htm'; 
    } else if ($flyOutIndex == 1) { 
     $flyOutURL = 'Content/HTMLSnippets/Flyouts/PriceHistory/price_history_comparisons.htm'; 
    } else if ($flyOutIndex == 2) { 
     $flyOutURL = 'Content/HTMLSnippets/Flyouts/PriceHistory/price_history_scenarios.htm'; 
    } else if ($flyOutIndex == 3) { 
     $flyOutURL = 'Content/HTMLSnippets/Flyouts/PriceHistory/price_history_analysis.htm'; 
    } 

    $('.current').removeClass('current'); 

    $(this).addClass('current'); 

    // Close any open flyouts 
    closeFlyout(); 

    $.ajax({ 
     type: 'GET', 
     url: DashboardApplicationRoot + $flyOutURL, 
     dataType: 'html', 
     cache: true, 
     success: function(data) { 

      $($widget).prepend(data); 

      $('.fly_container').animate({ 'right': '0' }, 300); 

      $('.scroll').jScrollPane(); 

      $('.striped li:nth-child(even)').addClass('odd'); 

     } 
    }); 

    return false; 

}); 

// Close fly out function 
$('.widget').delegate('.fly_container .close', 'click', function() { 

    closeFlyout(); 

    $('.current').removeClass('current'); 

    return false; 

}); 

回答

2

.delegate()檢查選擇在每次獲得一個事件,所以你可以這樣做時間:

$('.widget').delegate('.widget .fly_out:not(.foClose)', 'click', function() { 
    $(this).addClass('foClose'); 
    //rest of current code 
}); 

然後在你的親密委託聽這個新的選擇,以及:

$('.widget').delegate('.fly_container .close, .widget .foClose', 'click', function() { 
    $(this).removeClass('foClose'); 
    //rest of current code 
}); 

通過添加foClose類(或任何您想要命名的名稱),按鈕的單擊事件將由關閉委託偵聽來處理,而不是打開一個。如果點擊並按照這種方式處理,它將刪除foClass,再次將其作爲彈出鏈接。

+0

智能一個暱稱:) – RyanP13 2010-08-18 15:32:13

+0

使用相同的代碼我如何等待,直到當前飛出是否打開新的飛出之前,如果點擊一個鏈接是不是當前的? – RyanP13 2010-08-18 15:34:17

+0

@ RyanP13 - 你想排隊他們,或忽略其他''fly_out'點擊,而它的工作? – 2010-08-18 15:38:37

1

在飛出單擊上,測試以查看菜單是否具有類current。如果有,請關閉彈出窗口,不要運行ajax方法。

if ($(this).hasClass("current")) { 
    $(this).removeCLass("current"); 
    closeFlyout(); 
    return; 
}