【发布时间】:2017-02-25 20:07:35
【问题描述】:
我有一个获取图像尺寸的功能。我从 createObjectURL 发送返回。
它在获取尺寸方面效果很好,但我无法取回值。我正在尝试获取这两个值,但问题似乎是函数中有一个函数。并且外部函数不知道内部函数中设置的值。当我硬编码一个值时,如下面的“qq”就可以了。所以我可以看到问题不在返回值中,而是在值中。
在这种情况下你如何读取这些值?
imgSrc = window.URL.createObjectURL(this.files[0]);
var imgSize = getImgSize(imgSrc);
var newWidth = imgSize.retWidth;
var newHeight = imgSize.retHeight;
alert(newWidth);
alert(newHeight);
function getImgSize(imgSrc) {
var newImg = new Image();
newImg.onload = function() {
var nHeight = newImg.height;
var nWidth = newImg.width;
//alert('The image size is ' + width + '*' + height);
}
newImg.src = imgSrc; //this must be done AFTER setting onload
return {
retWidth: nWidth,
retHeight: 'qq'
};
}
【问题讨论】:
-
你的变量
nHeight和nWidth只存在于onload函数的范围内,请看下面的答案。
标签: javascript jquery css dom