Javascript 每次刷新页面时,随机链接?

Javascript 每次刷新页面时,随机链接?,javascript,html,random,logic,Javascript,Html,Random,Logic,我试图弄清楚,当页面从我的列表中随机刷新时,如何创建一个每次都会更改的锚定标记 假设我有这个清单 <a href="http://testpage.com/">This is the first one</a> <a href="http://testpage.com/">This is the second one</a> <a href="http://testpage.com/">This is the third one<

我试图弄清楚,当页面从我的列表中随机刷新时,如何创建一个每次都会更改的锚定标记

假设我有这个清单

<a href="http://testpage.com/">This is the first one</a>
<a href="http://testpage.com/">This is the second one</a>
<a href="http://testpage.com/">This is the third one</a>

这是第一个 这是第二个 这是第三个

这就像Adsense的链接单元广告,但我只希望它做简单的随机操作,而不是像Adsense那样做任何额外的工作,比如检查是否与主题相关

请告诉我我能做什么

谢谢

获取页面上所有脚本的详细信息(由于页面已加载到此点,因此在此之前以及包括此脚本在内的所有脚本):

将该列表中的最后一个脚本(即此脚本元素)存储在
此脚本中

var this_script = scripts[scripts.length - 1];
我将把下一行分成更小的部分。
给出介于
0
(包含)和
1
(独占)之间的值。将其乘以3将在
0
(包含)和
3
(独占)之间得到均匀分布,并将其截断。这将给出一个介于
0
2
之间的随机整数。如果向数组中添加元素,它将更新,因为它在计算中使用短语.length,而不是文字3:

Math.floor(Math.random()*phrases.length)
创建并返回一个实现文本接口的新节点,它的数据只是传入的值。在本例中,它是短语数组中的随机元素:

document.createTextNode(phrases[Math.floor(Math.random()*phrases.length)])
是不言自明的,在这种情况下,脚本元素的父元素将是(这由
表示,它包含两个参数,一个
新的\u子对象
和一个
旧的\u子对象
。我们为
新的子对象
传递了新的文本节点,为
旧的子对象
传递了此脚本,这导致此脚本从DOM中删除并替换为文本节点:

this_script.parentNode.replaceChild(document.createTextNode(phrases[Math.floor(Math.random()*phrases.length)]), this_script);

您可以像这样使用php:

<?php $linkName = mt_rand(1,3);
 if ($linkName == 1) echo '<a href="http://testpage.com/">This is the first one</a>';
 if ($linkName == 2) echo '<a href="http://testpage.com/">This is the second one</a>';
 if ($linkName == 3) echo '<a href="http://testpage.com/">This is the third one</a>';
?>


使用Math.random()并将每个标记分配到一个范围。但是使用php会更容易。锚列表来自哪里?JS数组?这太棒了!我会稍微了解一下,以便将来我自己也可以使用它!@Ali Great!:)如果你想让我把它分解一下并解释一下,请告诉我。你是如何利用JS数组基于0的特性,获得
random(0-1)的
floor
*array.length
为每个项目提供均等的机会+1@PaulP.R.O.我很乐意!我实际上在Seneca@York学习计算机编程,我真的很想知道更多…请注意,这个问题是关于JS的。
document.createTextNode(phrases[Math.floor(Math.random()*phrases.length)])
this_script.parentNode.replaceChild(document.createTextNode(phrases[Math.floor(Math.random()*phrases.length)]), this_script);
<?php $linkName = mt_rand(1,3);
 if ($linkName == 1) echo '<a href="http://testpage.com/">This is the first one</a>';
 if ($linkName == 2) echo '<a href="http://testpage.com/">This is the second one</a>';
 if ($linkName == 3) echo '<a href="http://testpage.com/">This is the third one</a>';
?>