我处于需要以这种方式解决的情况;需要将 a 转换local variable
为 a global variable
。有一个示例返回图像的真实宽度和高度,我从这个答案中找到了这些方法。.
需要将局部变量转换为全局变量pic_real_height
并pic_real_width
返回其真实值。
CSS:
img { width:0px; height:0px; }
jQuery :
console.log($('.imgCon img').height());//returns 0
var img = $('.imgCon img')[0]; // Get my img elem
var pic_real_width, pic_real_height;
$('<img/>').attr('src', $(img).attr('src')).load(function() {
pic_real_width = this.width;
pic_real_height = this.height;
console.log( pic_real_width + 'x' + pic_real_height );
// -- returns true 570x320 --
});
//problem starts here:
console.log( pic_real_width + 'x' + pic_real_height );
//returns undefined
// need to return this as an global variable 570x320