JavaScript+JSON问题:无法加载照片/undefined.jpg

JavaScript + JSON issue: not able to load photos/undefined.jpg

本文关键字:照片 undefined jpg 加载 问题 JavaScript+JSON      更新时间:2023-09-26

我在这个JavaScript代码中连接图像名称时遇到问题。它来自JSON,这让我作为一个初学者变得更加困难。如果有任何帮助,我将不胜感激!这是我的JS:

 function log(msg){
    console.log(msg)
 }
function createImage(file, parent){
    var str = file;
    var filename = ("photos/"+filename+".jpg");
    var image = new Image();
    image.src = filename;
    image.style.width = "50px";
    image.style.height = "auto";
		
    image.onload = function(){
        log('good ' + file );	
        parent.appendChild(image); //adds the image to the page!
    }
		
    image.onerror = function(){
        log('not able to load ' + filename );	
        //parent.appendChild(image);
    }
}
 

var文件名和文件混淆。)

function log(msg){
     console.log(msg)
}
function createImage(file, parent){
    var str = file; // unused
    var filename = ("photos/"+file+".jpg"); // filename was undefined here.
    var image = new Image();
    image.src = file;
    image.style.width = "50px";
    image.style.height = "auto";
    image.onload = function(){
        log('good ' + file );   
        parent.appendChild(image); //adds the image to the page!
    }
    image.onerror = function(){
        log('not able to load ' + filename );   
        //parent.appendChild(image);
    }
}