2017-10-11 121 views
2
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); 

大家好,我有以上代碼和我的目標是爲與屬性的陣列的圖像,但結果現在是jQuery的數組重複使用圖片

Results of array

JSFiddle

任何人都可以給我建議我做錯了什麼,謝謝!

+2

只有第一個作品,因爲當我是0,你正在推動DOM節點到數組,'IMG [ 1]'和以後是沒有意義的。您應該使用'imgArray.push(img)'來代替。 – Terry

回答

6

您有一個錯字。

imgArray.push(img[i]); 

應該是:

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

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);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>