html - 图片未加载到网页上
问题描述
当我在本地打开页面时,图像会加载,但是当我转到 ip 时,图像没有加载。该图像与 html 文件位于同一目录中。
<!DOCTYPE html>
<html>
<head>
<script type="text/JavaScript">
var img = new Image;
img.onload = function(){
var canvas = document.getElementById("can");
var context = canvas.getContext("2d");
context.drawImage(img, 0, 0);
setTimeout(loadImage, 1000);
};
function loadImage(){
img.src = 'image.jpg';
}
</script>
<title>Page</title>
</head>
<body onload="JavaScript:loadImage();">
<canvas id="can" width="1280" height="720" />
</body>
</html>
解决方案
我真的希望这个解决方案对你有用我刚刚通过添加斜杠符号编辑了图像的根并更改了 onload="loadImage();" 功能
<!DOCTYPE html>
<html>
<head>
<script type="text/JavaScript">
var img = new Image;
img.onload = function(){
var canvas = document.getElementById("can");
var context = canvas.getContext("2d");
context.drawImage(img, 0, 0);
setTimeout(loadImage, 1000);
};
function loadImage(){
img.src = '/image.jpg';
}
</script>
<title>Page</title>
</head>
<body onload="loadImage();">
<canvas id="can" width="1280" height="720" />
</body>
</html>
推荐阅读
- angularjs - 为什么在使用带有 AngularJS 的 Typescript 时需要“声明 var angular”?
- verilog - 这个循环如何在 SystemVerilog 语言中工作?
- java - 如何在不使用 xml 的 Java 类中格式化 Java 字符串
- java - 为什么我的类构造函数被重复调用?
- c# - 按期间分割忽略少数情况
- git - 如何浏览(cd ..)到父级直到找到特定文件夹?
- c# - 自动映射器在 System.Web.IHttpModule 上抛出 ReflectionTypeLoadException
- javascript - javascript试图去掉小数点
- css - sass中如何指定img文件的路径
- regex - 用于匹配和排除 ' 和 " 的正则表达式