如何更换一部分窗口.位置与随机生成的数字

How to replace a portion of window.location with a randomly generated number?

本文关键字:数字 随机 何更换 一部分 窗口 位置      更新时间:2023-09-26

我试图制作一款简单的游戏,其中选择是一种错觉。你有4个按钮从中挑选,北,西,东,南,和onclick我希望每个按钮生成一个随机数使用数学。随机,然后使用窗口。在同一次点击中将它们带到编号页面的位置。

。有100个标有1.html、2.html、3.html的HTML页面,一直到100. HTML,你从1.html页开始,往北点,随机生成的数字带你到23.html,再点,79.html,再点,3.html,以此类推。

我知道如何使用窗口。把你带到一个固定的页面,我知道怎么做数学。在1-100之间随机选择一个数字,但我不知道如何把这两个元素结合起来做我想做的事。

<html>
<head>
<script>
function beginner()
{
document.getElementById("demo1").innerHTML=(Math.floor(Math.random()*100)+1);
}
</script>

</head>
<body>
<div id="header" style="clear: both; margin-bottom: 25px;">
pick a direction to go
</div> 
<div id="nav" style="float: left; ">
<center>
<input type="button" id="north" value="north" onclick="window.location='demo1';" /> <br />
<input type="button" id="west" value="west" onclick="randompage2();" /> 
<input type="button" id="east" value="east" onclick="randompage3();" /> <br />
<input type="button" id="south" value="south" onclick="randompage4();" />
</center>
</div>
</body>
</html>

您可以使用+连接,所以我个人会这样做。

window.location = '/'+Math.floor(Math.random() * 100) + 1+'.html';

现在用JavaScript重定向用户的最佳方法是window.location.replace()(因为它将启动HTTP请求)

:

window.location.replace("http://example.com/pages/"+(Math.floor(Math.random()*100)+1)+".html")

这只是简单的字符串连接。阅读有关窗口的内容。MDN上的位置

希望我能帮上忙!