How to stop broken images showing

后端 未结 12 2377
谎友^
谎友^ 2021-01-02 12:08

I have a table with some images that I want to start out with no images in them.I have set the src=\"\".But when viewed in a browser it shows broken image pics.



        
12条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-02 12:39

    If you just want to get rid of broken image icon & don't want to hide the image and want to keep the resolution of it as is; Use a transparent base-64 image, to replace it.

    Jquery:

    $("img").on("error", function() {
        $(this).attr('src' ,'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=');
    });
    

    Pros:

    • No need to hide the image
    • No need to change Alt text of the image
    • base-64 image don't have specific height-width
    • works with jquery zoom (jquery.zoom.js), if the zoomed image is broken

提交回复
热议问题