收录日期:2020/06/02 19:05:34 时间:2013-05-29 21:54:09 标签:javascript,jquery,callback,slidetoggle

Here is the jQuery slideToggle function:

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

In the callback function I need to access current .class element (the one being clicked on). How can I do that?

Take a reference to the element outside of the callback, you can then use this inside the callback function.

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