Xhr响应返回不完整的对象

时间:2017-07-02 09:30:57

标签: javascript css ajax xmlhttprequest find

我会做xhr请求到一个url然后获取所有图像,添加一些属性,推入html页面。 这是我的简化代码:

function setStyle(image){
  var ratio = image.width/image.height;
  if(ratio>1){
    image.style.minHeight = '100%';
  }else{
    image.style.minWidth = '100%';
  }
  $('#gallery').innerHTML += image.parentNode.outerHTML;
}


function xhrRequest(){
  var xhr = new XMLHttpRequest();
  xhr.onload = function() {
    var data = this.response;
    if(data.indexOf('<img') !== -1){
      images = $(data).find('img');
      var len = images.length;
      for (var i = 0; i < len; i++) {
        setStyle(images[i]);
      }
    }
  }
  xhr.open("GET", "url");
  xhr.send(null);
}

我多次调用xhrRequest(),第一次使用find()函数获取的图像完美无缺,然后对象变得不完整,宽度和高度属性为空。

我很感激我能得到的任何帮助。

非常感谢!

0 个答案:

没有答案
相关问题