gpt4 book ai didi

Javascript 重定向网站日期

转载 作者:行者123 更新时间:2023-12-03 03:35:39 25 4
gpt4 key购买 nike

伙计们。我目前正在制作一个网站,但我想让我的网站自动重定向到星期几,而不需要按链接。示例:如果是星期一,网站将重定向到 monday.html。

我的代码:

<!DOCTYPE -be sure to use one of these! 
<html>
<head>
<title></title>
<script type="text/javascript">
window.onload = function(){chgDailyImg();}
function chgDailyImg()
{
var imagearray = new Array();
imagearray[0] = "sundaypic.png";
imagearray[1] = "mondaypic.png";
imagearray[2] = "tuesdaypic.png";
imagearray[3] = "wednesdaypic.png";
imagearray[4] = "assets/images/thursdaypic.png";
imagearray[5] = "fridaypic.png";
imagearray[6] = "saturdaypic.png";

var linkarray = new Array();
linkarray[0] = "sondag.html";
linkarray[1] = "mandag.html";
linkarray[2] = "tirsdag.html";
linkarray[3] = "onsdag.html";
linkarray[4] = "torsdag.html";
linkarray[5] = "fredag.html";
linkarray[6] = "lordag.html";

var textarray = new Array();
textarray[0] = "This is sunday's text - click to go to sunday's page";
textarray[1] = "This is monday's text - click to go to monday's page";
textarray[2] = "This is tuesday's text - click to go to tuesday's page";
textarray[3] = "This is wednesday's text - click to go to wednesday's page";
textarray[4] = "Torsdag";
textarray[5] = "This is friday's text - click to go to friday's page";
textarray[6] = "This is saturday's text - click to go to saturday's page";

var d = new Date(); /*** create a date object for use ***/
var i = d.getDay(); /*** use the date object to get the day of the week - this will be a number from 0 to 6 - sunday=0, saturday=6 -it's the way counting works in javascript it starts at 0 like in the arrays ***/
document.getElementById("dailyImg").src = imagearray[i];
document.getElementById("dailyLink").href = linkarray[i];
document.getElementById("dailyLink").innerHTML = textarray[i];
}
</script>
</head>
<body>
<div>
<a href="sundaypage.html" id="dailyLink">This is sunday's text - click to go to sunday's page</a>
<img src="sundaypic.jpg" alt="daily pic" title="daily pic" width="300" height="100" id="dailyImg" />
</div>
</body>
</html>

最佳答案

您所需要做的就是设置 window.location.href linkarray[i] 而不是将其写入页面:

<!DOCTYPE html>
<html>

<head>
<title></title>
<script type="text/javascript">
window.onload = function() {
chgDailyImg();
}

function chgDailyImg() {
var imagearray = new Array();
imagearray[0] = "sundaypic.png";
imagearray[1] = "mondaypic.png";
imagearray[2] = "tuesdaypic.png";
imagearray[3] = "wednesdaypic.png";
imagearray[4] = "assets/images/thursdaypic.png";
imagearray[5] = "fridaypic.png";
imagearray[6] = "saturdaypic.png";

var linkarray = new Array();
linkarray[0] = "sondag.html";
linkarray[1] = "mandag.html";
linkarray[2] = "tirsdag.html";
linkarray[3] = "onsdag.html";
linkarray[4] = "torsdag.html";
linkarray[5] = "fredag.html";
linkarray[6] = "lordag.html";

var textarray = new Array();
textarray[0] = "This is sunday's text - click to go to sunday's page";
textarray[1] = "This is monday's text - click to go to monday's page";
textarray[2] = "This is tuesday's text - click to go to tuesday's page";
textarray[3] = "This is wednesday's text - click to go to wednesday's page";
textarray[4] = "Torsdag";
textarray[5] = "This is friday's text - click to go to friday's page";
textarray[6] = "This is saturday's text - click to go to saturday's page";

var d = new Date(); /*** create a date object for use ***/
var i = d.getDay(); /*** use the date object to get the day of the week - this will be a number from 0 to 6 - sunday=0, saturday=6 -it's the way counting works in javascript it starts at 0 like in the arrays ***/

// Automatically redirect to the day's page
window.location.href = linkarray[i];
}
</script>
</head>

</html>

希望这有帮助! :)

关于Javascript 重定向网站日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45870524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com