如果html< img>的src属性被输入一个默认图像,无效? [英] Inputting a default image in case the src attribute of an html <img> is not valid?

查看:228
本文介绍了如果html< img>的src属性被输入一个默认图像,无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在HTML < img> 标记中呈现默认图片,以防< src 属性无效(仅使用HTML)?如果不是,那么解决它的轻量级方法是什么?

解决方案

您只需要一个 HTML解决方案...



<!c $ c><!DOCTYPE html PUBLIC - // W3C // DTD HTML 4.01 // ENhttp: //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.pngtype =image / png> < img src =https://appharbor.com/assets/images/stackoverflow-logo.png/> < / body>< / 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&lt; img&gt;的src属性被输入一个默认图像,无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆