HTML5 <img>标记替代图像

Ham*_*med 5 html html5 image

我只是想知道在html img标签中

<img alt = "default image" src = "image"> 是否有可能有一个替代图像而不仅仅是文本.或者是否可以将img标记嵌套在alt""中.

j08*_*691 13

不,你不能把另一个图像放在一个的alt属性中<img>.但是,您可以使用onerror事件使用JavaScript加载另一个图像,因此如果未加载原始图像,则可以加载备份.

function standby() {
    document.getElementById('foo').src = 'https://www.google.com/images/srpr/logo11w.png'
}
Run Code Online (Sandbox Code Playgroud)
<img id="foo" src="notfound.gif" onerror="standby()">
Run Code Online (Sandbox Code Playgroud)

  • 这更像是一个黑客而不是一个答案,但你可以做到这一点(不是你应该做的!):<img rel="nofollow noreferrer" src ="image1"alt ="image2"onerror ="this.onerror = null; this.src = this. ALT;" > (2认同)