问题
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
回答1:
Set the load
handler first, then set the src
.
fullImage.bind('load', function() {
...
}).attr('src', fullImageUrl);
回答2:
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>
来源:https://stackoverflow.com/questions/4921712/load-event-with-jquery-fails-in-ie-8