0

画像が正しく読み込まれるかどうかを確認する方法を知っている人はいますか。画像をプリロードするスクリプトがあります。画像が存在しない場合、スクリプトが終了しないので、属性「src」をデフォルト値にします。

image.load(function(){
 //it never reache here if the image doesn't exist
}).attr('src','a non existing image');

ありがとう

4

1 に答える 1

2
var image = $('<img>').appendTo('body');

image.load(function() {
    // image is found and loaded
}).error(function() {
    // image not loaded
    image.attr('src','default-image.jpg');
}).attr('src','a non existing image');
于 2009-12-06T11:58:36.143 に答える