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

后端 未结 22 2562
渐次进展
渐次进展 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:21

    If you're using Angular/jQuery then this might help...

    
    

    Explanation

    Assuming that item has a property url that might be null, when it is then the image will show up as broken. That triggers execution of onerror attribute expression, as described above. You need to override the src attribute as described above, but you will need jQuery to access your altSrc. Couldn't get it to work with vanilla JavaScript.

    Might seem a little hacky but saved the day on my project.

提交回复
热议问题