【发布时间】:2015-10-08 23:43:58
【问题描述】:
我正在尝试制作一个简单的游戏,其中选择是一种幻觉。您有 4 个按钮可供选择,北、西、东和南,并且 onclick 我希望每个按钮使用 math.random 生成一个随机数,然后使用 window.location 在同一次单击中将它们带到该编号页面。
即有100个html页面标记为1.html、2.html、3.html,一直到100.html,你从1.html页面开始,点击北,随机生成的数字带你到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>
【问题讨论】:
-
这里不能使用简单的字符串连接吗?
window.location = Math.floor(Math.random() * 100) + 1) + '.html';
标签: javascript button random window.location