如果html< img>的src属性被输入一个默认图像,无效? [英] Inputting a default image in case the src attribute of an html <img> is not valid?
问题描述
有没有办法在HTML < img>
标记中呈现默认图片,以防<
您只需要一个 HTML解决方案...
由于第一张图像不存在,会显示回退(堆栈溢出标志)。如果您正在使用不支持 object
,它会忽略该标记并使用 img
标记。有关兼容性,请参阅 caniuse 网站。截至2018年,这个元素得到了来自ie6 +的所有浏览器的广泛支持。
Is there any way to render a default image in an HTML <img>
tag, in case the src
attribute is invalid (using only HTML)? If not, what would be your lightweight way to work around it?
You asked for an HTML only solution...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Object Test</title>
</head>
<body>
<object data="http://stackoverflow.com/does-not-exist.png" type="image/png">
<img src="https://appharbor.com/assets/images/stackoverflow-logo.png" />
</object>
</body>
</html>
Since the first image doesn't exist, the fallback (the Stack Overflow logo) will display. And if you're using a really old browser that doesn't support object
, it will just ignore that tag and use the img
tag. See caniuse website for compatibility. As of 2018 this element is widely supported by any and all browsers from ie6+.
这篇关于如果html< img>的src属性被输入一个默认图像,无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!