2009-08-07 52 views
2

這裏是jQuery的功能的slideToggle:的jQuery的slideToggle()回調函數

$('.class').click(function() { 
    $(this).parent().next().slideToggle('slow', function() { 
     // how can I access $('.class') that was clicked on 
     // $(this) returns the $(this).parent().next() which is the element 
     // that is currently being toggled/slided 
    }); 
}); 

在回調函數中我需要訪問當前的.class元素(被點擊的那個)。我怎樣才能做到這一點?

回答

15

引用回調之外的元素,然後可以在回調函數中使用它。

$('.class').click(function() { 
    var $el = $(this); 
    $(this).parent().next().slideToggle('slow', function() { 
     //use $el here 
    }); 
}); 
+0

謝謝:)我沒有想到這樣簡單的解決方案。 – 2009-08-07 15:34:32