jQuery slideToggle() 回调函数

3

这里是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 元素(即被单击的元素)。我该如何做到这一点?
1个回答

19

在回调函数外部获取元素的引用,然后在回调函数内部使用该引用。

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

谢谢 :) 我没有想到这么简单的解决方案。 - Richard Knop

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接