【发布时间】:2015-04-02 00:52:32
【问题描述】:
我试图让下面的这个 php 脚本自动在下面将页面快速重新加载到包含网站页面的文件夹中的随机页面。我的代码只是显示一个文本链接或什么都没有,并且不会自动重新加载到随机页面。代码如下:
<?php
//set the urls
$urls = array("1.html" ,"2.html" ,"3.html" ,"4.html" ,"5.html" ,"6.html" ,"7.html" ,"8.html" ,"9.html"
,"10.html" ,"11.html" ,"12.html" ,"13.html" ,"14.html" ,"15.html" ,"16.html" ,"17.html" ,"18.html" ,
"19.html" ,"20.html" ,"21.html" ,"22.html" ,"23.html" ,"24.html" );
//set the number in (rand()%3); for however many links there are
$random = (rand()%24);
echo ("<a href = \"$urls[$random]");
?>
因此,如果我有一个用户在 3.html 上的页面,并且他们单击了一个按钮,我希望脚本运行并从该数组中加载另一个随机页面,然后在 URL 框中将页面重新加载到该页面.
3.html长这样,这里是按钮自动页面加载javascript的代码:
<h2 id="correct">CORRECT</h2>
<h2 id="wrong">WRONG</h2>
<div id="answers">
<button onclick="document.getElementById('wrong').style.visibility='visible';
window.location = 'random.php';" id="one">1986</button>
<button onclick="document.getElementById('correct').style.visibility='visible';
window.location = 'random.php';" id="two">1913</button>
<button onclick="document.getElementById('wrong').style.visibility='visible';
window.location = 'random.phprandom.php';" id="three">1723</button>
<button onclick="document.getElementById('wrong').style.visibility='visible';
window.location = 'random.php';" id="four">1812</button>
</div>
请帮我修复代码!
此外,如果链接用数字标记,是否有更简单的方法可以将这些页面添加到数组中。意思是我可以用一行代码做到这一点吗?
【问题讨论】:
-
(我只是想让你知道
array_rand()是一个东西) -
如果你的网址总是这样格式化,你能不能只得到一个介于 1 和页数之间的随机数,然后将“.html”附加到它以让你的网址重定向到?可能更容易?
标签: javascript php jquery html random