我对 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();
最佳答案
这是解决问题的另一种方式。
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();
关于javascript - 根据一天中的时间转到新的 html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36099059/