我从具有多个图像选项的API中提取数据,我有一个函数,我想从其他几个名为validateBoxArt
的函数调用,这些函数选择可用的最大图像的URL。
我在另一个函数中调用它是这样的:
$.each(data.results, function(index, game) {
validateBoxArt(game);
$('#dynamic-content').append('<a href="title.html?id=' + game.id + '">' +
'<div class="row game-preview">' +
'<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1 col-sm-12">' +
'<img src="' + boxArt + '" class="game-preview-image">' +
'<h2 style="display:inline-block; padding-left:5px;" class="post-title">' + game.name + ' <span class="small">' + year + '</span></h2>' +
'</div>' +
'</div>' +
'</a>' +
'<hr>');
});
}
功能本身:
function validateBoxArt(game) {
if (game.image.super_url != undefined) {
var boxArt = staticUrl+game.image.super_url;
}
else if (game.image.large_url != undefined) {
var boxArt = staticUrl+game.image.large_url;
}
else if (game.image.medium_url != undefined) {
var boxArt = staticUrl+game.image.medium_url;
}
else if (game.image.small_url != undefined) {
var boxArt = staticUrl+game.image.small_url;
}
else{
var boxArt = 'http://placehold.it/500x350';
}
console.log(boxArt);
return boxArt;
}
第一个函数中的box art返回一个错误,表示未定义,但变量出现在控制台日志中,如何在初始函数中检索变量,以便定义它?
答案 0 :(得分:2)
您需要将从validateBoxArt()
重新调整的字符串分配给变量:
var boxArt = validateBoxArt(game);