根据一天中的时间转到新的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();
murli2308

您只需要使用重定向页面 window.location.href

function getIndex() {
  var currentTime = new Date().getHours();
  if (0 <= currentTime&&currentTime < 5) {
      window.location.href = 'night.html';
  } else if (5 <= currentTime&&currentTime < 11) {
      window.location.href = 'morning.html';
  } else if (11 <= currentTime&&currentTime < 16) {
     window.location.href = 'day.html';
  } else if (16 <= currentTime&&currentTime < 22) {
    window.location.href = 'evening.html';
  } else if (22 <= currentTime&&currentTime <= 24) {
    window.location.href = 'night.html';
  }
}

getIndex();

剩下的所有代码都可以

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章