Passing $(this) to a callback in jQuery?

前端 未结 3 1208
Happy的楠姐
Happy的楠姐 2020-12-11 10:16

I have two effects that I want to run one after another, but I can\'t work out how to pass $(this) to the callback from the first effect.

This is what I

相关标签:
3条回答
  • 2020-12-11 10:23

    Give this a shot:

    var self = $(this);
    if(self.hasClass('flag')) {
        $('someElement').slideUp(function() {
            self.slideDown();
        });
    }
    
    0 讨论(0)
  • 2020-12-11 10:29

    cache it like this... just don't forget thevar.

    var $this = $(this);
    if($this.hasClass('flag')) {
        $('someElement').slideUp(function() {
            $this.slideDown();
        });
    }
    
    0 讨论(0)
  • 2020-12-11 10:41

    Save the reference.

    if($(this).hasClass('flag')) {
        var el = this;
        $('someElement').slideUp(function() {
            $(el).slideDown();
        });
    }
    
    0 讨论(0)
提交回复
热议问题