【发布时间】:2018-06-21 18:19:39
【问题描述】:
我有以下工作代码:
// Get photos from file and load first initial photo
$.ajax({
type: "GET",
url: "photos_gps.json",
success: initialPhoto,
error: handleError
});
function initialPhoto(data){
console.log(data);
var img_tag = "<img id=" + '"photoBox" src=' + "'photos/" + data[0].Filename + "' />";
console.log("Img_tag: " + img_tag);
$('#mainBox').prepend(img_tag);
photos = data;
setTimeout(function() {
console.log($('#photoBox').height());
console.log($('#photoBox').width());
}, 1000);
}
首先我正在加载一个包含图像信息的文件。 成功后,我选择第一个图像并将其添加到 DOM 元素中。当我在没有 setTimeOut 函数的情况下记录图像的宽度和高度时,值将同时为 0 和 setTimeOut 函数 3648 和 5472。
我想摆脱 setTimeOut 函数,所以我尝试了以下回调函数:
$('#mainBox').prepend(img_tag, function() {
console.log($('#photoBox').height());
console.log($('#photoBox').width());
});
这会导致在图像下方添加以下文本:
function() { console.log($('#photoBox').height()); console.log($('#photoBox').width()); }
看来我还不懂回调...
【问题讨论】:
标签: jquery ajax image callback prepend