我是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();
您只需要使用重定向页面 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();
剩下的所有代码都可以
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句