我对JavaScript很陌生,所以我想知道你们中是否有人可以解决这个难题。这显然是完全错误的,但希望你能看到我需要的东西。 我需要index.html根据时间重定向到新的.html文件。
function getIndex() {
var currentTime = new Date().getHours();
if (0 <= currentTime&¤tTime < 5) {
document.write("night.html");
}
if (5 <= currentTime&¤tTime < 11) {
document.write("morning.html");
}
if (11 <= currentTime&¤tTime < 16) {
document.write("day.html");
}
if (16 <= currentTime&¤tTime < 22) {
document.write("evening.html");
}
if (22 <= currentTime&¤tTime <= 24) {
document.write("night.html");
}
}
getIndex();
答案 0 :(得分:2)
这是另一种处理事情的方式。
function getIndex() {
var d = new Date();
var h = d.getHours();
var pages = ['night', 'morning', 'day', 'evening', 'night'];
var eTimes = [0, 5, 11, 16, 22, 24];
for (var i = 0; i < eTimes.length - 1; i++) {
if (eTimes[i] <= h && h < eTimes[i + 1]) {
window.location.href = pages[i] + '.html';
}
}
}
getIndex();
答案 1 :(得分:1)
您只需使用window.location.href
function getIndex() {
var currentTime = new Date().getHours();
if (0 <= currentTime&¤tTime < 5) {
window.location.href = 'night.html';
} else if (5 <= currentTime&¤tTime < 11) {
window.location.href = 'morning.html';
} else if (11 <= currentTime&¤tTime < 16) {
window.location.href = 'day.html';
} else if (16 <= currentTime&¤tTime < 22) {
window.location.href = 'evening.html';
} else if (22 <= currentTime&¤tTime <= 24) {
window.location.href = 'night.html';
}
}
getIndex();
让你所有的代码都很好