首页 > 解决方案 > 图片未加载到网页上

问题描述

当我在本地打开页面时,图像会加载,但是当我转到 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>

标签: 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>


推荐阅读