Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax响应和匿名函数作用域_Ajax_Jquery - Fatal编程技术网

Ajax响应和匿名函数作用域

Ajax响应和匿名函数作用域,ajax,jquery,Ajax,Jquery,在下面的代码中 var entryTemplate = document.getElementById('entryTemplate'); entryTemplate = entryTemplate.firstChild; for (var ipost in posts) { var post = posts[ipost]; var clone = entryTemplate.cloneNode(true); clone = $(clone); if (post.imageU

在下面的代码中

var entryTemplate = document.getElementById('entryTemplate');
entryTemplate = entryTemplate.firstChild;

for (var ipost in posts)
{
  var post = posts[ipost];
  var clone = entryTemplate.cloneNode(true);
  clone = $(clone);

  if (post.imageURL)
  {
    var imgElement = document.createElement('img');
    var largeImageURL = post.largeImageURL ? post.largeImageURL : post.imageURL;
    imgElement.src = post.thumbPresent ? POST_THUMB_URL + '/' + post.postID : largeImageURL;
    imgElement.alt = '';
    clone.find('div.BlogImageURL a').attr('href', largeImageURL).text(largeImageURL);
    clone.find('div.BlogImage a').attr('href', imgElement.src).append(imgElement);

    // get bytesize
    var postdata = 'inline_image_url=' + encodeURIComponent(post.imageURL);
    postdata += '&linked_image_url=' + encodeURIComponent(post.largeImageURL);
    $.ajax({
    type: 'POST',
    url: ASYNC_GET_BYTESIZE_URL,
    data: postdata,
    success: function(bytesize) {
      clone.find('.BlogImageBytesize').html(bytesize);
    }
    });
  }
  else
  {
    clone.find('div.BlogImageURL').text('(This post contains no images)');
    clone.find('div.BlogImage').remove();
  }

  $('#outputDiv').append(clone);
}

clone.find('.BlogImageBytesize').html(bytesize);
所有ajax响应(粗体线)都会修改最后一个克隆,这可能是因为当第一个响应到达并且克隆指向最后一个克隆时,循环就完成了

我怎样才能解决这个问题


谢谢。

也许您可以将
clone
设置为ajax调用的
上下文。(见)然后,我认为它会像这样工作:

$.ajax({
    type: 'POST',
    url: ASYNC_GET_BYTESIZE_URL,
    data: postdata,
    context: clone,
    success: function(bytesize) {
      $(this).find('.BlogImageBytesize').html(bytesize);
    }
});

我不确定
上下文是否必须是一个普通的DOM元素,或者它是否可以是一个jQuery对象,但希望这能让您走上正确的轨道。

谢谢,这非常有效!在我的例子中,clone已经是jquery对象,所以我使用这个.find('.BlogImageBytesize').html(bytesize);