Html HTML5 <img> 标签替代图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28504753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML5 <img> tag alternative image
提问by Hammad Ahmed
I was just wondering that in the html img tag
我只是想知道在 html img 标签中
<img alt = "default image" src = "image">
is it possible to have a alternate image rather than just text. Or if is it possible to nest the img tag in the alt ""s.
<img alt = "default image" src = "image">
是否可以有替代图像而不仅仅是文本。或者是否可以在 alt "" 中嵌套 img 标签。
回答by j08691
No, you can't put another image in the alt attribute of an <img>
. You can however use the onerror event to load another image using JavaScript, so if the original image doesn't load, a backup can be loaded.
不,您不能在 .alt 属性中放置另一个图像<img>
。但是,您可以使用 onerror 事件使用 JavaScript 加载另一个图像,因此如果原始图像未加载,则可以加载备份。
function standby() {
document.getElementById('foo').src = 'https://www.google.com/images/srpr/logo11w.png'
}
<img id="foo" src="notfound.gif" onerror="standby()">
回答by Moz
As far as I know, the alt attribute is supposed to have only text, since it is helpful for screen readers to "read" images.
据我所知, alt 属性应该只有文本,因为它有助于屏幕阅读器“阅读”图像。
If what you are searching for is a alternate image when the main one fails, you can do so with some JavaScript.
如果您要搜索的是主图像失败时的备用图像,则可以使用一些 JavaScript 来实现。
On this question you can see how to use the onError usage:
在这个问题上你可以看到如何使用 onError 用法: