我的问题是我想让math.random生成一个数字,然后使用该数字从列表中选择一个网站。但我只想让它循环一次。我怎么能这样做?
可以更多地指定它我有这些网站,我想与一个从我的列表中随机网站的按钮链接。但我希望它只采用每个网站。
function myFunction() {
if (Math.floor((Math.random() * 35) + 1)== 1) {
window.location.href = "Sang1.html";
}
if (Math.floor((Math.random() * 35) + 1)== 2) {
window.location.href = "Sang2.html";
}
if (Math.floor((Math.random() * 35) + 1)== 3) {
window.location.href = "Sang3.html";
}
if (Math.floor((Math.random() * 35) + 1)== 4) {
window.location.href = "Sang4.html";
}
if (Math.floor((Math.random() * 35) + 1)== 5) {
window.location.href = "Sang5.html";
}
if (Math.floor((Math.random() * 35) + 1)== 6) {
window.location.href = "Sang6.html";
}
if (Math.floor((Math.random() * 35) + 1)== 7) {
window.location.href = "Sang7.html";
}
if (Math.floor((Math.random() * 35) + 1)== 8) {
window.location.href = "Sang8.html";
}
if (Math.floor((Math.random() * 35) + 1)== 9) {
window.location.href = "Sang9.html";
}
if (Math.floor((Math.random() * 35) + 1)== 10) {
window.location.href = "Sang10.html";
}
if (Math.floor((Math.random() * 35) + 1)== 11) {
window.location.href = "Sang11.html";
}
if (Math.floor((Math.random() * 35) + 1)== 12) {
window.location.href = "Sang12.html";
}
if (Math.floor((Math.random() * 35) + 1)== 13) {
window.location.href = "Sang13.html";
}
if (Math.floor((Math.random() * 35) + 1)== 14) {
window.location.href = "Sang14.html";
}
if (Math.floor((Math.random() * 35) + 1)== 15) {
window.location.href = "Sang15.html";
}
if (Math.floor((Math.random() * 35) + 1)== 16) {
window.location.href = "Sang16.html";
}
if (Math.floor((Math.random() * 35) + 1)== 17) {
window.location.href = "Sang17.html";
}
if (Math.floor((Math.random() * 35) + 1)== 18) {
window.location.href = "Sang18.html";
}
if (Math.floor((Math.random() * 35) + 1)== 19) {
window.location.href = "Sang19.html";
}
if (Math.floor((Math.random() * 35) + 1)== 20) {
window.location.href = "Sang20.html";
}
if (Math.floor((Math.random() * 35) + 1)== 21) {
window.location.href = "Sang21.html";
}
if (Math.floor((Math.random() * 35) + 1)== 22) {
window.location.href = "Sang22.html";
}
if (Math.floor((Math.random() * 35) + 1)== 23) {
window.location.href = "Sang23.html";
}
if (Math.floor((Math.random() * 35) + 1)== 24) {
window.location.href = "Sang24.html";
}
if (Math.floor((Math.random() * 35) + 1)== 25) {
window.location.href = "Sang25.html";
}
if (Math.floor((Math.random() * 35) + 1)== 26) {
window.location.href = "Sang26.html";
}
if (Math.floor((Math.random() * 35) + 1)== 27) {
window.location.href = "Sang27.html";
}
if (Math.floor((Math.random() * 35) + 1)== 28) {
window.location.href = "Sang28.html";
}
if (Math.floor((Math.random() * 35) + 1)== 29) {
window.location.href = "Sang29.html";
}
if (Math.floor((Math.random() * 35) + 1)== 30) {
window.location.href = "Sang30.html";
}
if (Math.floor((Math.random() * 35) + 1)== 31) {
window.location.href = "Sang31.html";
}
if (Math.floor((Math.random() * 35) + 1)== 32) {
window.location.href = "Sang32.html";
}
if (Math.floor((Math.random() * 35) + 1)== 33) {
window.location.href = "Sang33.html";
}
if (Math.floor((Math.random() * 35) + 1)== 34) {
window.location.href = "Sang34.html";
}
if (Math.floor((Math.random() * 35) + 1)== 35) {
window.location.href = "Sang35.html";
}
if (Math.floor((Math.random() * 35) + 1)== 36) {
window.location.href = "Sang36.html";
}
答案 0 :(得分:6)
将网站的网址放在一个数组中。随机播放阵列。按顺序从混洗数组中选择网站。您将最多选择一个网站一次,没有重复。
答案 1 :(得分:1)
首先创建一个包含所有URL的数组
var urls = ["Sang1.html", "Sang2.html", "Sang3.html"]; // add all URLs here ...
现在从数组中随机选择一个
var randomIndex = Math.floor(Math.random() * (urls.length - 1));
var url = urls[randomIndex];