开发者

AJAX requests and THIS

开发者 https://www.devze.com 2023-01-06 23:48 出处:网络
I\'m having a little problem when trying to target an A tag inside a $(\'div\').fadeOut() callback. I got this

I'm having a little problem when trying to target an A tag inside a $('div').fadeOut() callback.

I got this

开发者_运维问答$('.anchor').click(function() {
    $('#div').fadeOut(null, showDiv);
    $('#div').fadeIn();
    return false;
});

showDiv is an AJAX request which I use $(this) inside it trying to refer to the A tag clicked but I noticed that it refers to the div #div

What I want to do is to do some AJAX request when a linkis clicked, but I want it to happen only after the DIV's fade out. This way, the request would populate #div and the new content will only show up when it fades in again.


You can change the context using $.proxy(), like this:

$('.anchor').click(function() {
  $('#div').fadeOut(null, $.proxy(showDiv, this));
  $('#div').fadeIn();
  return false;
});

You can test it here, or do the same thing manually:

$('.anchor').click(function() {
  var self = this;
  $('#div').fadeOut(null, function() {
    showDiv.apply(self);
  });
  $('#div').fadeIn();
  return false;
});
0

精彩评论

暂无评论...
验证码 换一张
取 消