使用图像

时间:2017-10-11 19:01:46

标签: javascript jquery arrays push each

var imgLink = ['ONYX', 'Bristol_Blue_B_002_10', 'Oakhampton_B_001_10-1', 'Quartet_KitchenCountertop_500x342', 'Eternal-Serena_RS11277_Silestone-Kitchen', 'zodiaq_provence_kitchen_2200x1467-b94f5-1', 'Eternal-Serena_RS11277_Silestone-Kitchen-1', 'Ecobycosentino-', 'Hanstone', 'IceStone-Forest-Fern-Shadowlight-Vignette-Kitchen-Countertop', 'RS833_Silversilk_OA-hpr-copy_CMYK', 'scalea'];

    var imgArray = [];

jQuery.each(imgLink, function(i){
    var img = jQuery('<img/>')

    .attr("src", "http://www.link.com/wp-content/uploads/2017/10/" +imgLink[i]+ ".jpg")
    imgArray.push(img[i]);  
});
console.log(imgArray);

大家好,我上面有一个代码,我的目标是制作带有属性的数组图像,但现在的结果是

Results of array

JSFiddle

任何人都可以告诉我我做错了什么,谢谢!

2 个答案:

答案 0 :(得分:6)

你有一个错字。

imgArray.push(img[i]);  

应该是:

imgArray.push(img); // <-- img is not an array

答案 1 :(得分:2)

&#13;
&#13;
var imgLink = ['ONYX', 'Bristol_Blue_B_002_10', 'Oakhampton_B_001_10-1', 'Quartet_KitchenCountertop_500x342', 'Eternal-Serena_RS11277_Silestone-Kitchen', 'zodiaq_provence_kitchen_2200x1467-b94f5-1', 'Eternal-Serena_RS11277_Silestone-Kitchen-1', 'Ecobycosentino-', 'Hanstone', 'IceStone-Forest-Fern-Shadowlight-Vignette-Kitchen-Countertop', 'RS833_Silversilk_OA-hpr-copy_CMYK', 'scalea'];

var imgArray = [];

jQuery.each(imgLink, function(key, value) {
  var img = jQuery('<img/>').attr("src", "http://www.link.com/wp-content/uploads/2017/10/" + value + ".jpg")

  // uncomment below for img 
  //imgArray.push(img);

  // just html for testing
  imgArray.push(img[0]);
});
console.log(imgArray);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;