我想随机链接到任何页面x,这是我到目前为止编写的代码,它不起作用......任何建议?
var page1link = "pages/sketches.php";
var page2link = "pages/photography.php";
var page3link = "pages/defined.php";
var page4link = "pages/spectacle2.php";
var list = [page1link, page2link,page3link, page4link];
var number = list[Math.floor(Math.random()*list.length)];
var randomlink =list[number];
function myFunction(){
document.getElementById("mylink").href = 'randomlink';
};
<body onload="myFunction()">
<container>
<h1>
<a href='randomlink' id='mylink'> Click Me</a>
</h1>
</container>
</body>
答案 0 :(得分:0)
试试这个:
var page1link = "pages/sketches.php";
var page2link = "pages/photography.php";
var page3link = "pages/defined.php";
var page4link = "pages/spectacle2.php";
var list = [page1link, page2link,page3link, page4link];
var number = Math.floor((Math.random() * list.length));
var randomlink = list[number];
function myFunction(){
document.getElementById("mylink").href = 'randomlink';
};
答案 1 :(得分:0)
试试这个..
<!DOCTYPE html>
<html>
<body >
<container>
<h1>
<a href="#" id='mylink'> Click Me</a>
</h1>
</container>
<script>
function myFunction(){
var page1link = "pages/sketches.php";
var page2link = "pages/photography.php";
var page3link = "pages/defined.php";
var page4link = "pages/spectacle2.php";
var list = [page1link, page2link,page3link, page4link];
var randomlink =list[Math.floor(Math.random()*list.length)];
document.getElementById("mylink").href = randomlink;
}
window.onload = myFunction;
</script>
</body>
</html>