代码之家  ›  专栏  ›  技术社区  ›  Richard Knop

jQuery slideToggle()回调函数

  •  2
  • Richard Knop  · 技术社区  · 15 年前

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

    在回调函数中,我需要访问current.class元素(被单击的元素)。我该怎么做?

    1 回复  |  直到 11 年前
        1
  •  19
  •   redsquare    15 年前

    引用回调函数外部的元素,然后可以在回调函数内部使用它。

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