Fallback background-image if default doesn't exist

后端 未结 5 727
忘掉有多难
忘掉有多难 2020-11-29 09:43

I want to set an image as a background, however the image name might be either bg.png or bg.jpg.

Is there any non-javascript way to create

5条回答
  •  臣服心动
    2020-11-29 10:26

    You can have background image with css and use the same image URL in a hidden image tag and onError, change the image URL and make it show it

    { e.currentTarget.src = brokenImageFallbackUrl; e.currentTarget.style.display = "block"; // or keep this image hidden and set the backgroundImage of the parent div }} />

提交回复
热议问题