jquery无法再Chrome下获得图片的长宽的解决方案

来源:岁月联盟 编辑:exp 时间:2012-04-01

遇到个需要获得图片的长宽的问题。在IE、FireFox下均正常,就TMD在Chrome中不行,网上非有人说可以,然后还归结为是不是Chrome版本问题。

终于的终于,找到了答案。

在大多数情况下,把js代码放到图片标签的后面:


[html]
<img id="test" src="**.jpg" /> 
<script> 
alert(document.getElementById("test").width;<pre name="code" class="html"></script> 
<img id="test" src="**.jpg" />
<script>
alert(document.getElementById("test").width;<pre name="code" class="html"></script>
这样是可以得到图片的宽度的,但是在Chrome下就是不行。

甚至在jquery中:


[html]
$(document).ready(function(){ 
alert(document.getElementById("test").width; 
}); 
$(document).ready(function(){
alert(document.getElementById("test").width;
});
这样,Chrome照样说不行,我C。

然后,只有下面的方法Chrome才点头:


[html]
$(window).load(function(){ 
 alert($("#test").width()); 
}); 
$(window).load(function(){
 alert($("#test").width());
});

 

摘自  cangkukuaimanle的专栏