在 DOM 加载后,我无法让 Chrome 识别图像的宽度或高度。图像通过 phpThumb 脚本动态加载(调整图像大小)。如果我去掉动态 url 并将其替换为图像的直接 url,我不会遇到任何问题,并且 Chrome 中一切正常,但使用动态 url,chrome 似乎无法计算图像宽度或高度。
有人有这方面的经验吗?它让我头疼。
有问题的代码是:
var theImage = new Image();
theImage.src = $image.attr('src');
var imgwidth = theImage.width;
var imgheight = theImage.height;
其中 imgwidth = 0;对于 chrome,但 IE、Firefox 都报告正确的大小。
最佳答案
正确的代码是 .onload 和以下函数:
var theImage = new Image();
theImage.src = $image.attr('src');
theImage.onload = function(){
var imgwidth = $(this).width;
var imgheight = $(this).height;
});
关于jQuery;对于 new Image(),Chrome 图像宽度和高度 = 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5198094/