image naturalWidth返回零

men*_*mam 19 javascript

image naturalWidth返回零......就是这样,为什么?

var newimage = new Image();
newimage.src = 'retouche-hr' + newlinkimage.substring(14,17) + '-a.jpg'; 
var width = newimage.naturalWidth;
alert (width);
Run Code Online (Sandbox Code Playgroud)

帮助,我不知道为什么!

***那道路很好,图像显示出来!

Gre*_*reg 40

我想这是因为你不是在等待图片加载 - 试试这个:

var newimage = new Image();
newimage.src = 'retouche-hr' + newlinkimage.substring(14,17) + '-a.jpg'; 
newimage.onload = function()
{
    var width = this.naturalWidth;
    alert(width);
}
Run Code Online (Sandbox Code Playgroud)

  • 设置`src`和`onload`的顺序对于要调用的加载回调很重要. (5认同)