找不到src源图像时,如何静默隐藏“找不到图像”图标

您是否知道在找不到图像文件时如何从呈现的HTML页面中隐藏经典的“未找到图像”图标?

任何使用JavaScript / jQuery / CSS的工作方法?


慕姐8265434
浏览 770回答 3
3回答

婷婷同学_

要隐藏每个图像错误,只需在页面底部(恰好在body标记之前)添加以下JavaScript:(function() {&nbsp; &nbsp; var allimgs = document.images;&nbsp; &nbsp; for (var i = 0; i < allimgs.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; allimgs[i].onerror = function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.style.visibility = "hidden"; // Other elements aren't affected.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }})();
打开App,查看更多内容
随时随地看视频慕课网APP