2011-05-19 53 views
2

我想显示图像只有当它们加载成功(一些路径给404)。 有以下代码,它工作正常。JQuery:显示图片时,成功加载和冲突与加载

<script> 
    $(document).ready(function() { 
     $(".complex-image").load(function(){ 
      $(this).show(); 
     }); 
    }); 
</script> 

现在,一个页面中显示了谷歌地图,其中规定:

onload = initMap 
onunload = GUnload 

在这个页面在IE8和Chrome,图像不显示,即使可用。但在Firefox 4中显示正常。我的网站上有图片+地图的示例页面是this

我也尝试了反向方法:加载时出现错误时隐藏图像。使用以下代码:

<script> 
    $(document).ready(function() { 
     $(".complex-image").ready(function(){ 
      $(this).hide(); 
     }); 
    }); 
</script> 

这不会隐藏包含地图和非地图页面上IE8上的图像。

当定义了body onload时,什么是成功加载时显示图像的跨浏览器方式?

编辑:即使正从缓存中读取图像用于Elzo的解决方案,并获得img.load()解雇了,用这个: jQuery callback on image load (even when the image is cached)

回答

4
+0

我认为另一种方法是检查图像对象不是null或undefined。好吧,它不像你的例子那么流行,但它也应该起作用。 – reporter 2011-05-19 13:12:46

+0

谢谢!为了让img.load()在从缓存读取图像时触发,使用[link]增强了解决方案(http://stackoverflow.com/questions/3877027/jquery-callback-on-image-load-even-当最图像是缓存的) – tarkeshwar 2011-05-19 15:34:25