首页 > 解决方案 > 我在同一个文件夹中有 HTML 文件,但它们不起作用。为什么?

问题描述

我在本地机器 Jen_MoodBoard 上的同一个 HTML 文件夹中有 4 个文件,这是我的代码:

        <script>
            document.getElementById("outdoorPhotography").onclick=function () {
                location.href = "/Jen_MoodBoard/OutdoorPhotography.html";
            };
            document.getElementById("childAbusePrevention").onclick=function () {
                location.href = "https://preventchildabuse.org/";
            };
            document.getElementById("sfGiants").onclick=function () {
                location.href = "https://www.mlb.com/giants";
            };
            document.getElementById("fireSafety").onclick=function () {
                location.href = "https://www.youtube.com/user/FireKillsCampaign/videos";
            };
            document.getElementById("electroMusic").onclick=function () {
                location.href = "https://soundcloud.com/alltrapnation";
            };
            document.getElementById("snapCreation").onclick=function () {
                location.href = "https://snap.berkeley.edu/snapsource/snap.html#present:Username=eagledragon&ProjectName=BND_of_Doom";
            };
            document.getElementById("divLlama").onclick=function () {
                location.href = "/Jen_MoodBoard/Jen_Anime2.html";
            };
            document.getElementById("gameList").onclick=function () {
                location.href = "https://gamestarmechanic.com/workshop";
            };
            document.getElementById("citationsButton").onclick=function () {
                location.href = "/Jen_MoodBoard/Jen_Moodboard_Citations.html";
            };
        </script>

我用 JavaScript 将它们全部链接到按钮中,但是当我用另一台计算机进行测试时,/Jen_MoodBoard/OutdoorPhotography.html、/Jen_MoodBoard/Jen_Anime2.html 和 /Jen_MoodBoard/Jen_Moodboard_Citations.html 导致页面显示

Your file was not found
It may have been moved or deleted.
ERR_FILE_NOT_FOUND

我检查了链接,它们对我来说看起来不错,但是为什么指向我的页面的链接不起作用而其余的却起作用?谢谢!

标签: javascripthtmlhyperlink

解决方案


看起来您在家用计算机上没有服务器的情况下运行此程序,并且 Jen_MoodBoard 不在您的根目录中。所以你需要使用相对路径。在不知道您的目录结构的情况下,我将使用几个假设。

如果带有上述脚本的文件与 OutdoorPhotography.html 等位于同一目录中(即 Jen_MoodBoard)。

然后将链接从“/Jen_MoodBoard/OutdoorPhotography.html”更改为“./OutdoorPhotography.html”

如果带有上述脚本的文件与 Jen_MoodBaoard 位于不同的目录中,则使用 Jen_MoodBoard 的相对路径。例如“./Jen_MoodBoard/OutdoorPhotography.html”


推荐阅读