HTML if image is not found

后端 未结 16 2009
温柔的废话
温柔的废话 2020-12-07 13:46

I have an image in a HTML page:

\"Smiley

If the image is not found o

相关标签:
16条回答
  • 2020-12-07 14:09

    The best way to solve your problem:

    <img id="currentPhoto" src="SomeImage.jpg" onerror="this.onerror=null; this.src='Default.jpg'" alt="" width="100" height="120">
    

    onerror is a good thing for you :)

    Just change the image file name and try yourself.

    0 讨论(0)
  • 2020-12-07 14:09

    For the alternative, if the image doesn't exist - show nothing at all. (what I was looking for)

    You can swap the function from Robby Shaw's answer in the "onerror" attribute to "this.remove()".

    <img id="currentPhoto" src="SomeImage.jpg" alt='1' width="100" height="120">
    <img id="currentPhoto" src="SomeImage.jpg" onerror="this.onerror=null; this.remove();" alt="2" width="100" height="120">

    0 讨论(0)
  • 2020-12-07 14:16

    Ok but I like to use this way, so that whenever original image is not available you can load your default image that may be your favorite smiley or image saying Sorry ! Not Available, But in case if both the images are missing you can use text to display. where you can also you smiley then. have a look almost covers every case.

    <img src="path_to_original_img/img.png"  alt="Sorry! Image not available at this time" 
           onError="this.onerror=null;this.src='<?=base_url()?>assets1/img/default.jpg';">
    
    0 讨论(0)
  • 2020-12-07 14:17

    You can show an alternative text by adding alt:

    <img src="my_img.png" alt="alternative text" border="0" /> 
    
    0 讨论(0)
  • 2020-12-07 14:18

    its works for me that if you dont want to use alt attribute if image break then you can use this piece of code and set accordingly.

    <h1>
      <a> 
       <object data="~/img/Logo.jpg" type="image/png">
         Your Custom Text Here
       </object>
      </a>
    </h1>
    
    0 讨论(0)
  • 2020-12-07 14:19

    simple way to handle this, just add an background image.

    0 讨论(0)
提交回复
热议问题