如果在 HTML 上找不到第一个 img,请备份

Backup img if the first can't be found on HTML

我要显示img1.png。但是,如果查找文件时出错,我希望显示备份 missing.png。在 HTML 中有没有办法拥有 if 语句或异常?

<try>?
    <img src="img1.png">
<except error 404>?
    <img src="missing.png">

不明白的地方我都打上了问号

查看答案here

它说你可以按照下面的方式做

<img src="img1.png" alt="Image not found" onError="this.onerror=null;this.src='missing.png';" />

其中 onerror 是一个 Javascript 函数。有关 onerror

的更多详细信息,请参阅 docs