在AJAX请求完成后添加jquery操作

时间:2017-07-27 20:21:34

标签: javascript php jquery ajax

我正在运行一个AJAX脚本来加载滚动的旧帖子。这是有效的,但我想要的是做一个img视网膜替换。这适用于非AJAX请求,但是当使用AJAX加载帖子时,它无效。

下面是我用来加载AJAX帖子的jQuery脚本。

function imagesHD() {
  var imagesHD = $("img[data-1x]");
  if (window.devicePixelRatio === 2) {
    $.each(imagesHD, function() {
      var $this = $(this);

      $this.attr("src", $this.data("2x"));
    });
  } else {
    $.each(imagesHD, function() {
      var $this = $(this);

      $this.attr("src", $this.data("1x"));
    });
  }
}

jQuery(function($){
  var canBeLoaded = true, // this param allows to initiate the AJAX call only if necessary
      bottomOffset = 2000; // the distance (in px) from the page bottom when you want to load more posts
  $(window).scroll(function(){
    var data = {
      'action': 'loadmore',
      'query': onlineincasso_loadmore_params.posts,
      'page' : onlineincasso_loadmore_params.current_page
    };
    if( $(document).scrollTop() > ( $(document).height() - bottomOffset ) && canBeLoaded == true ){
      $.ajax({
        url : onlineincasso_loadmore_params.ajaxurl,
        data:data,
        type:'POST',
        beforeSend: function( xhr ){
          // you can also add your own preloader here
          // you see, the AJAX call is in process, we shouldn't run it again until complete
          canBeLoaded = false; 
        },
        success:function(data){
          if( data ) {
            $('.main').find('.content:last-of-type').after( data ); // where to insert posts
            canBeLoaded = true; // the ajax is completed, now we can run it again
            onlineincasso_loadmore_params.current_page++;
          }
        },
        complete:function(xhr) {
          imagesHD();
        }
      });
    }
  });
});

你知道我怎么能让这个工作吗?抱歉我缺乏jquery / ajax知识:(

0 个答案:

没有答案