我试着做一个简单的游戏,选择是一种幻想。您有4个按钮可供选择,从北,西,东和南,我希望每个按钮生成一个随机数使用math.random,然后使用window.location带他们到那个编号的页面在同一点击。
也就是说,有100个html页面标记为1.html、2.html、3.html,一直到100.html,从1.html页面开始,单击north,随机生成的数字显示为23.html,再次单击,79.html,再次单击,3.html等等。
我知道如何使用window.location将您带到一个设置的页面,我也知道如何做math.random来选择介于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>
发布于 2015-07-19 22:28:43
你可以和一个+连在一起,所以我会亲自这么做。
window.location = '/'+Math.floor(Math.random() * 100) + 1+'.html';
发布于 2015-07-19 22:28:55
现在,用JavaScript重定向用户的最好方法是window.location.replace()
(因为它将启动一个HTTP请求)
所以:
window.location.replace("http://example.com/pages/"+(Math.floor(Math.random()*100)+1)+".html")
这只是简单的字符串连接。阅读关于window.location 论MDN的文章
希望我能帮忙!
https://stackoverflow.com/questions/31506555
复制相似问题