Javascript 设置变量在页面加载时不起作用,但通过控制台起作用?

Javascript 设置变量在页面加载时不起作用,但通过控制台起作用?,javascript,jquery,console.log,Javascript,Jquery,Console.log,我一定错过了什么。我的脚本如下: var slideshowUL = jQuery('div.slideshow').css('overflow', 'hidden').children('ul'), imgs = slideshowUL.find('img'), imgWidth = imgs[0].width, imgsLen = imgs.length, current = 0; console.log(slideshowUL); cons

我一定错过了什么。我的脚本如下:

var slideshowUL = jQuery('div.slideshow').css('overflow', 'hidden').children('ul'),
    imgs = slideshowUL.find('img'),
    imgWidth = imgs[0].width,
    imgsLen = imgs.length,
    current = 0;

    console.log(slideshowUL);
    console.log(imgs);
    console.log(imgWidth);
    console.log(imgsLen);
    console.log(current);
输出为:

[ul, prevObject: p.fn.p.init[1], context: document, selector: "div.slideshow.children(ul)", constructor: function, init: function…]
[img, img, img, img, prevObject: p.fn.p.init[1], context: document, selector: "div.slideshow.children(ul) img", constructor: function, init: function…]
0
4
0 

它无法获取图像的宽度。当我将上述代码直接粘贴到控制台中时,它会返回正确的值(750)。

零宽度表示您在加载图像之前读取该值。在窗口onload和notready/inline时调用它

图像尚未加载。加载图像时必须询问宽度。

<代码>$(窗口).load(函数(){…})