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

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

    This works well for me. Maybe you wanna use JQuery to hook the event.

     
    

    Updated with jacquargs error guard

    Updated: CSS only solution I recently saw Vitaly Friedman demo a great CSS solution I wasn't aware of. The idea is to apply the content property to the broken image. Normally :after or :before do not apply to images, but when they're broken, they're applied.

    
    
    

    Demo: https://jsfiddle.net/uz2gmh2k/2/

    As the fiddle shows, the broken image itself is not removed, but this will probably solve the problem for most cases without any JS nor gobs of CSS. If you need to apply different images in different locations, simply differentiate with a class: .my-special-case img:before { ...

提交回复
热议问题