我的jquery代码:
img[i]='img-src';
$("#popimage1").attr("src",img[i]);
alert($("#popimage1").width()); //doesn't give the width of the current image, but it gives for the previous, and for first null
Run Code Online (Sandbox Code Playgroud)
我的HTML代码:
<img src="" id="popimage1"/>
Run Code Online (Sandbox Code Playgroud)
所以概念是我用jquery为每个i加载不同的图像src,然后我用宽度和高度做一些计算.问题是我用前一个计数器i得到了图像的值.通过推杆我做了一个临时的解决方案
$("#popimage1").hide();
setTimeout(alert($("#popimage1").width(),2000);
$("#popimage1").show();
Run Code Online (Sandbox Code Playgroud)
但它总是不起作用,并导致不必要的超时.而不是警报有另一个使用img宽度和高度的函数,我只是为了你的方便写了警报.任何帮助赞赏!
Mic*_*ior 10
您必须先等待图像加载.
试试这个.
img[i] = "img-src";
$("#popimage1").attr("src", img[i]).load(function() {
alert($("#popimage1").width());
});
Run Code Online (Sandbox Code Playgroud)