簡體   English   中英

腳本從 Live Server 加載,但在打開 index.html 時未加載

[英]Script is loaded from Live Server but not when opening index.html

我是 JS 新手。 我在 VS Code 中使用 vanilla JS 制作了一個簡單的 Snake 游戲。 代碼中有最小的 CSS,所以我把它放在 html 中。

當我從帶有 Live Server 的 VS Code ( http://127.0.0.1:5500/ )打開index.html時,它工作正常。

但是當我從文件資源管理器( file:///D:/Prog/Javascript/VanillaJS_projects/Snake/index.html )打開它時,只有 html 被加載,沒有出現蛇和食物碎片。 如果我嘗試使用htmlpreview.github.io打開它,也會發生同樣的情況

這是索引。html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Snake</title>
    <script src="game.js" defer type="module"></script>
    <style>
        ...
    </style>
    </head>
    <body>
        <div id="game-board"></div>
    </body>
</html>

無論我使用什么瀏覽器,我都會得到相同的結果。

有什么區別? 為什么不能正常加載?

您的瀏覽器遵守內容安全政策

如果您打開file:/// URL,您可能會將瀏覽器視為窮人的文件查看器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM