jQuery的;新图片的Chrome图片宽度和高度= 0() [英] Jquery; Chrome Image Width and Height = 0 for new Image()

查看:145
本文介绍了jQuery的;新图片的Chrome图片宽度和高度= 0()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让Chrome在DOM加载后识别图片的宽度或高度。该图像是通过phpThumb脚本(调整图像大小)动态加载的。如果我拿走了动态网址,并将其替换为图片的直接网址,我没有遇到任何问题,并且所有内容都可以在Chrome中使用,但通过动态网址,Chrome似乎无法计算图片的宽度或高度。



任何人都有这方面的经验?它正在做我的头。



有问题的代码是:

  var theImage = new Image(); 
theImage.src = $ image.attr('src');
var imgwidth = theImage.width;
var imgheight = theImage.height;



解决方案

正确的代码是.onload和以下函数:

  var theImage = new Image(); 
theImage.src = $ image.attr('src');
theImage.onload = function(){
var imgwidth = $(this).width;
var imgheight = $(this).height;
});


Im having trouble getting Chrome to recognize an Image width or height after the DOM has loaded. The Image is dynamically loaded through phpThumb script (which resizes the image). If I take away the dynamic url and just replace it with the Image's direct url I get no issues and everything works in Chrome but with the dynamic url, chrome doesn't seem to be able to calculate the image width or height.

Anyone have any experience with this? Its doing my head in.

The code in question is :

var theImage     = new Image();
theImage.src     = $image.attr('src');
var imgwidth     = theImage.width;
var imgheight    = theImage.height;

Where imgwidth = 0; for chrome, yet IE, Firefox both report the correct size.

解决方案

The right code is .onload and the following function:

var theImage     = new Image();
theImage.src     = $image.attr('src');
theImage.onload = function(){
    var imgwidth     = $(this).width;
    var imgheight    = $(this).height; 
});

这篇关于jQuery的;新图片的Chrome图片宽度和高度= 0()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆