Javascript文件路径链接不正确
Javascript file path not linking correctly
我试图在我的main.js文件夹拉不同的样式表取决于一天的时间。但是,我一直在控制台收到一个文件未找到错误。
当我打开inspect元素。.css文件出现在DOM中,但在控制台中,我收到一个文件未找到错误,文件路径不正确。
浏览器显示路径如下:文件:///用户/名字/文件/目录/foodclock day.css
,但它应该是:文件:///用户/名字/文件/目录/foodclock/css/day.css
——这是我的Javascript代码----
function getStylesheet() {
var currentTime = new Date().getHours();
if (0 <= currentTime&¤tTime < 5) {
document.write("<link rel='stylesheet' href='night.css' type='text/css'>");
}
if (5 <= currentTime&¤tTime < 11) {
document.write("<link rel='stylesheet' href='morning.css' type='text/css'>");
}
if (11 <= currentTime&¤tTime < 16) {
document.write("<link rel='stylesheet' href='day.css' type='text/css'>");
}
if (16 <= currentTime&¤tTime < 22) {
document.write("<link rel='stylesheet' href='evening.css' type='text/css'>");
}
if (22 <= currentTime&¤tTime <= 24) {
document.write("<link rel='stylesheet' href='night.css' type='text/css'>");
}
}
getStylesheet();
对解决这个问题有什么建议吗?提前感谢!
在每个"href"元素中添加"css/"。这样的:
function getStylesheet() {
var currentTime = new Date().getHours();
if (0 <= currentTime&¤tTime < 5) {
document.write("<link rel='stylesheet' href='css/night.css' type='text/css'>");
}
if (5 <= currentTime&¤tTime < 11) {
document.write("<link rel='stylesheet' href='css/morning.css' type='text/css'>");
}
if (11 <= currentTime&¤tTime < 16) {
document.write("<link rel='stylesheet' href='css/day.css' type='text/css'>");
}
if (16 <= currentTime&¤tTime < 22) {
document.write("<link rel='stylesheet' href='css/evening.css' type='text/css'>");
}
if (22 <= currentTime&¤tTime <= 24) {
document.write("<link rel='stylesheet' href='css/night.css' type='text/css'>");
}
}
getStylesheet();
你需要在所有的href链接前指定css/,因为css样式表存在于不同的目录css
function getStylesheet() {
var currentTime = new Date().getHours();
if (0 <= currentTime&¤tTime < 5) {
document.write("<link rel='stylesheet' href='css/night.css' type='text/css'>");
}
if (5 <= currentTime&¤tTime < 11) {
document.write("<link rel='stylesheet' href='css/morning.css' type='text/css'>");
}
if (11 <= currentTime&¤tTime < 16) {
document.write("<link rel='stylesheet' href='css/day.css' type='text/css'>");
}
if (16 <= currentTime&¤tTime < 22) {
document.write("<link rel='stylesheet' href='css/evening.css' type='text/css'>");
}
if (22 <= currentTime&¤tTime <= 24) {
document.write("<link rel='stylesheet' href='css/night.css' type='text/css'>");
}
}
getStylesheet();
相关文章:
- 显示中的不可见链接:可见
- 如何正确链接JQuery并使用.getJSON
- 为什么图片不是链接
- 为什么't我的脚本(正确链接的)不起作用
- Ajax不处理链接
- 外部Javascript不起作用,即使我正确链接了它(至少我很确定这一点)
- 表单帖子不接受链接
- JavaScript 函数不显示链接地址
- SugarCRM新鲜库存安装样式和脚本未正确链接
- 我的jQuery没有正确链接
- 加载图像而不是链接
- 如果在鼠标悬停事件中触发了鼠标关闭,则立即返回,并且不执行链接的鼠标退出事件
- 正则表达式查找以文本而不是链接形式编写的 URL
- 正则表达式 - 不后跟 - 链接
- 如何正确链接Angular HttpPomise
- 正确链接承诺
- jQuery slideDown,不处理链接
- 项目的类似Facebook的按钮不是链接
- 应该't$parser/$formatter在不同的位置运行,而不是链接
- 可以'似乎无法正确链接外部java脚本