jQuery: Unable to fade out a previously faded in element

折月煮酒 提交于 2019-12-24 10:49:56

问题


I want to show a notification at the top – for 2 seconds – telling me which version of jQuery & jQuery UI was loaded. Unfortunately, I can't seem to be able to hide it later.

My code

$('<div>jQuery v' + jQuery.fn.jquery + ' and jQuery UI v' +  jQuery.ui.version + ' loaded.</div>')
.addClass('ui-state-highlight').prependTo('body').hide(0, function() {
  $(this).fadeIn(500, function() {
    setTimeout(function() {
      $(this).fadeOut(500, function() {
        $(this).remove();
      });
    }, 2000);
  });
});

jQuery Lint says I'm doing something wrong – which is true –, but I don't know how to do it the right way.


回答1:


It is probably a scope problem. Try:

  $(this).fadeIn(500, function() {
    var parentContext = $(this);
    setTimeout(function() {
      parentContext.fadeOut(500, function() {
        $(this).remove();
      });
    }, 2000);
  });


来源:https://stackoverflow.com/questions/4479247/jquery-unable-to-fade-out-a-previously-faded-in-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!