【发布时间】:2020-11-03 22:10:59
【问题描述】:
我正在做以下事情:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, minimum-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<script>
function LoadImage() {
var rowCover = document.createElement("img");
var ImgSRC = "https://marell-av.nl/thumbnails/A/A.jpg"
rowCover.setAttribute("src", ImgSRC);
rowCover.setAttribute("style", "margin-left: 5px; margin-right:10px; margin-top: 5px; height:200px; width:200px; float:left");
//target.appendChild(rowCover);
document.getElementById('tracks').appendChild(rowCover);
}
</script>
</head>
<body>
<div id="tracks"></div>
<script>
LoadImage()
</script>
</body>
</html>
问题是该图像在 Firefox 中可以正常加载,但不能在 chrome 或 Edge 中加载。为什么? 当我检查 Chrome 中的代码时,链接很好。当我将链接“https://marell-av.nl/thumbnails/A/A.jpg”粘贴到新标签中时,图像加载正常。
作为旁注,我还想知道如何在加载失败的情况下加载占位符图像。
【问题讨论】:
-
建议..与其在运行时创建元素,不如在html中创建并隐藏它,除非加载失败
-
ImgSRC定义为imgSRC。为什么不发布也显示附加部分的实际代码?要检查加载是否失败,您有事件 onerror -
当您尝试使用this method 时会发生什么?
-
占位符?阅读
altdeveloper.mozilla.org/en-US/docs/Web/HTML/Element/img 的规范 -
https://marell-av.nl和这段代码是同一个站点吗?如果是这样,您可能不需要其中的完整 URL。如果没有,它会在您粘贴链接时要求登录。
标签: javascript html image google-chrome