问题描述
我试图根据一天中的时间在main.js文件夹中提取不同的样式表.但是,我一直在控制台中收到找不到文件"错误.
I'm trying to pull different stylesheets in my main.js folder depending on the time of day. However, I keep receiving a file not found error in the console.
当我打开检查元素时. day.css文件出现在DOM中,但是在控制台中,我收到一个文件未找到错误,并且文件路径不正确.
When I open up inspect element. The day.css file appears in the DOM, but in the console I receive a file not found error and the file path is incorrect.
浏览器将路径显示为:文件:///Users/myname/Documents/directory/foodclock/day.css
The browser shows the path as:file:///Users/myname/Documents/directory/foodclock/day.css
但是应该是:文件:///Users/myname/Documents/directory/foodclock/css/day.css
but what it should be is:file:///Users/myname/Documents/directory/foodclock/css/day.css
---这是我的Javascript代码----
---This is my Javascript code----
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();
是否有解决此问题的建议?预先感谢!
Any suggestions for troubleshooting this issue?Thanks in advance!
推荐答案
您需要在所有href链接前面指定css/,因为css样式表位于不同的目录css
You need to specify css/ in front of all href links because the css stylesheets are present in different directory 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();
这篇关于Javascript文件路径未正确链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!