2011-02-07 47 views
10

我寫了一個jQuery插件來縮放圖像。在ie 8中,大圖像的加載事件失敗。我嘗試像thsi:在jQuery中加載事件失敗IE 8

 var fullImage = container.find(options.fullSelector); 
     fullImage.attr('src', fullImageUrl).bind('load', function() { 
      content.fadeOut(options.fadeSpeed, function(){ 
       if(slideContent.size()){ 
        slideContent.slideUp(options.resizeSpeed, function(){ 
         smallImage.hide(); 
         fullImage.show(); 
         fullImage.parent().andSelf().stop().animate({ width: options.fullWidth + 'px' }, options.resizeSpeed); 
        }); 
       } 
       else{ 
        smallImage.hide(); 
        fullImage.show(); 
        fullImage.parent().andSelf().stop().animate({ width: options.fullWidth + 'px' }, options.resizeSpeed); 
       } 
      }); 
     }); 

錯誤說:對象不支持屬性或方法。

我在做什麼錯?

謝謝

+0

第一行後,鍵入'警報(fullImage.length);`,以確定是否發現實際上找到的東西。當涉及到遍歷時,IE8確實有一些怪癖。 – karim79 2011-02-07 13:25:39

+0

在第二個音符上,IE8並不開心加載任何東西。我很驚訝,它甚至允許下載其他瀏覽器:D – Shrinath 2011-02-07 13:31:37

回答

49

首先設置load處理程序,然後設置src

fullImage.bind('load', function() { 
    ... 
}).attr('src', fullImageUrl); 
0

這是怎麼回事?

var test = function($what) { 
 
    $('#debug').html('Image loaded: '+$what.width+' x '+$what.height); 
 
    console.log($what); 
 
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script> 
 
// Random image 
 
var src = "http://placehold.it/300x" + Math.round(Math.random() * (310 - 100) + 100); 
 
</script> 
 
<div id="debug">Image loading:</div>  
 
<img onload="test(this)" id="img" /> 
 

 
<script> 
 
$('#img')[0].src = src; 
 
</script>