javascript - 我在同一个文件夹中有 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
我检查了链接,它们对我来说看起来不错,但是为什么指向我的页面的链接不起作用而其余的却起作用?谢谢!
解决方案
看起来您在家用计算机上没有服务器的情况下运行此程序,并且 Jen_MoodBoard 不在您的根目录中。所以你需要使用相对路径。在不知道您的目录结构的情况下,我将使用几个假设。
如果带有上述脚本的文件与 OutdoorPhotography.html 等位于同一目录中(即 Jen_MoodBoard)。
然后将链接从“/Jen_MoodBoard/OutdoorPhotography.html”更改为“./OutdoorPhotography.html”
如果带有上述脚本的文件与 Jen_MoodBaoard 位于不同的目录中,则使用 Jen_MoodBoard 的相对路径。例如“./Jen_MoodBoard/OutdoorPhotography.html”
推荐阅读
- javascript - 在 htmlunit 中设置 Javascript 超时的问题
- pandas - 尝试绘制 DataFrame 时缓冲区的维数错误
- pyspark-dataframes - 在pyspark中将字符串转换为日期
- python - 提取单词直到某个符号
- prolog - Prolog 中的大于和小于值
- ssl - open.connection(x, "rb") 中的错误:schannel:接收握手失败,SSL/TLS 连接失败 [R studio]
- python - 为什么我的代码不适用于循环和 if 条件?
- excel - python脚本将行从1个excel表粘贴到另一个。粘贴应该是“按值粘贴”
- python - 如何在使用不同 np 形状的地方使用 np
- sql - 根据奇数和偶数将 2 列分为 4