javascript - 在 <img> 标签上捕获未知图片并设置默认图像

标签 javascript html image onerror

我有这个图片标签:

<img id="viewerofinstore" src="{{filteredArray[imageIndex]}}" 
onError="this.style.visibility = 'hidden'"   
onload="this.style.visibility = 'visible'"  width="297px" height="297px" />

图像的src每秒钟都会随着其他图像发生变化,但也有一些图像磨损并出现未知图像的情况。

我添加了 "onError="this.style.visibility = 'hidden'" 但有时我可以在隐藏之前看到未知图像一秒钟。

我想在显示之前捕获未知图像并放置默认图像或隐藏图像。

最佳答案

您可以将 img 默认隐藏,只有在正确加载时才可见。

<img id="viewerofinstore" src="{{filteredArray[imageIndex]}}" style="visibility:hidden"   
onload="this.style.visibility = 'visible'"  width="297px" height="297px" />

关于javascript - 在 <img> 标签上捕获未知图片并设置默认图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15458255/

相关文章:

javascript - 如何在 JavaScript 中使用撇号和逗号连接数组

javascript - 将 fp-ts TaskEither 与右侧的 Either 链接起来

image - 从模型图像中删除包标签

javascript - 正则表达式匹配字符串,如果不匹配

javascript - 传单弹出宽度

javascript - 如何拆分具有连续大写和小写字母的字符串(以获得分子的原子数)?

html - 使用 CSS 在图像上显示图像 : Position tags query

html - 如何删除悬垂的div

javascript - 允许随时选中 1 个 "JS generated"复选框

css - Img 在移动 css 中拉伸(stretch)