jQuery slideToggle()回调函数

Ric*_*nop 2 javascript jquery callback 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
    });
});
Run Code Online (Sandbox Code Playgroud)

在回调函数中,我需要访问当前的.class元素(被点击的元素).我怎样才能做到这一点?

red*_*are 16

引用回调之外的元素,然后可以在回调函数中使用它.

$('.class').click(function() {
    var $el = $(this);
    $(this).parent().next().slideToggle('slow', function() {
         //use $el here
    });
});
Run Code Online (Sandbox Code Playgroud)