javascript - 根据一天中的时间转到新的 html 文件

标签 javascript html

我对 JavaScript 很陌生,所以我想知道你们中是否有人可以解决这个难题。这显然是完全错误的,但希望你能明白我的需要。 我需要 index.html 根据一天中的时间重定向到新的 .html 文件。

function getIndex() {
var currentTime = new Date().getHours();
if (0 <= currentTime&&currentTime < 5) {
document.write("night.html");
}
if (5 <= currentTime&&currentTime < 11) {
document.write("morning.html");
}
if (11 <= currentTime&&currentTime < 16) {
document.write("day.html");
}
if (16 <= currentTime&&currentTime < 22) {
document.write("evening.html");
}
if (22 <= currentTime&&currentTime <= 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/

相关文章:

jquery - 单击下拉列表(元素后元素)

Javascript:让函数等待查询完成

javascript - Phonegap iPad 应用启动画面/启动屏幕在设备就绪时发生变化

javascript - 两个类之间的平滑动画(特别是字体系列标签)

html - CSS 两个网格 DIV,图像和文本让我停电

javascript - 过滤json数据并在网格中显示数据

javascript - 使用 Chart.js 在折线图网格上自定义水平线 Y 轴标记的插件

javascript - C3.js 条形图 : setting X axis labels

javascript - jquery 一个一个加载div,不自动循环

javascript - 检测用户输入语言javascript