2016-07-05 63 views
-2

我想隨機鏈接到任何頁面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

有你'聲明隨時隨地randomizer'?如果是這樣,它有什麼作用? –

+0

''randomlink'!== randomlink'除非'randomlink ='randomlink';' – epascarello

+0

Javascript數組沒有size()方法。 – nnnnnn

回答

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'; 
}; 
+0

它不工作...未找到 在此服務器上未找到請求的網址/個人網站/ randomlink。 –

+0

確定它工作...謝謝 –

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>