Inputting a default image in case the src attribute of an html

后端 未结 22 2553
渐次进展
渐次进展 2020-11-22 16:02

Is there any way to render a default image in an HTML tag, in case the src attribute is invalid (using only HTML)? If not, what would

22条回答
  •  甜味超标
    2020-11-22 16:18

    Well!! I found this way convenient , check for the height attribute of image to be 0, then you can overwrite the src attribute with default image: https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image

     image.setAttribute('src','../icons/.png');
      //check the height attribute.. if image is available then by default it will 
      //be 100 else 0
      if(image.height == 0){                                       
           image.setAttribute('src','../icons/default.png');
      }
    

提交回复
热议问题