load event with jquery fails in IE 8

2019-02-12 16:02发布

i've written a jquery plugin to scale a image up an back down. in ie 8 the load event of the large version of the image fails. i tried like thsi:

        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);
                }
            });
        });

the error says: Object doesn't support property or method.

what am i doing wrong?

thank you

2条回答
仙女界的扛把子
2楼-- · 2019-02-12 16:17

What about this?

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>

查看更多
啃猪蹄的小仙女
3楼-- · 2019-02-12 16:25

Set the load handler first, then set the src.

fullImage.bind('load', function() {
   ...
}).attr('src', fullImageUrl);
查看更多
登录 后发表回答