<html>
<head>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
getImgWH("http://lailailailai.yungler.com/196/media/img/9efa87c0f465288e.jpg");
function getImgWH(path) {
var realwidth;
$("<img/>").attr("src", path).load(function() {
realwidth = this.width;
alert(realwidth); //160
});
alert(realwidth); //undefined
}
</script>
<head>
<body></body>
</html>
代码如上,在回调函数内,可以正常给realwidth赋值,但是出了回调函数之后realwidth的值就变成了undefined,求解要怎么才能获取到realwidth。