我在同一文件夹中有HTML文件,但它们无法正常工作。为什么?

时间:2019-03-22 22:56:13

标签: javascript html hyperlink

我在本地计算机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

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

1 个答案:

答案 0 :(得分:0)

好像您在家用计算机上没有服务器的情况下运行此程序,并且Jen_MoodBoard不在您的根目录中。因此,您需要使用相对路径。在不知道您的目录结构的情况下,我将进行一些假设。

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

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

如果带有上述脚本的文件与Jen_MoodBaoard所在的目录不同 然后使用相对路径到Jen_MoodBoard。 例如“ ./Jen_MoodBoard/OutdoorPhotography.html”