jQuery: Check if image exists

后端 未结 6 694
予麋鹿
予麋鹿 2020-11-27 17:20

I\'m loading an image path via jQuery $.ajax and before showing the image I\'d like to check if it in fact exists. Can I use the image load/ready event or something similar

6条回答
  •  北海茫月
    2020-11-27 18:11

    Well, you can bind .error() handler...

    like this,

    $(".myimage").error(function(){
      $(this).hide();
    });
    

    well, yours is okay already with load-event

    $(".myimage").load(function() {
        $(this).show();
    });
    

    the problem with this is if Javascript was disabled the image will not ever show...

提交回复
热议问题