2015-09-13 22 views
0

我嘗試做一個井字遊戲,我有2個按鈕(單人遊戲和2個玩家),我需要通過爲用戶選擇(depanding上的按鈕一個int挑),並在我的JS更改值,
我的html:傳遞參數的onLoad funtion(HTML和JS)

<body onload="startGame(i);"> //this is the parameter i want to use 
     <table> 
      // code...... 
     </table> 

     </br> 
     <div id= "menu" class="animated bounceInLeft"> <!-- Buttons --> 

      <a href = "javascript:startGame(1);" class = "animated shake">Single player</a> // the single player button 
      <a href = "javascript:startGame(2);" class = "animated shake">Two players</a> // to 2 players button 
     </div> 
</body> 
</html> 

JS:

var choise = 0; //global 

function startGame(i){ 
    if (i == 1) 
     choise = 1;  //change according to parameter 
    else 
     choise = 1; 
} 

這可能嗎?

謝謝!

回答

0

你不能傳遞用戶選擇響應文件加載的值。文件加載不是響應選擇而發生的事情。 (除非選擇是在前一頁上進行的,在這種情況下,您必須以某種方式將值傳遞給新文檔 - 例如查詢字符串或本地存儲 - 然後從那裏讀取它。)

您要麼等待點擊按鈕,要麼給參數添加一個值。 (在問題的代碼中,onload屬性中的i未定義)。

+0

我希望頁面根據按下ofcourse ..我如何管理該按鈕改變? – Coder123

+1

如果通過「更改」您的意思是「加載新頁面」,那麼您使用常規鏈接而不是JavaScript。如果你的意思是「修改現有的DOM」,那麼你可以讓你的JavaScript函數做一些更有趣的事情來設置一個變量(並且理想的情況是瞭解'addEventListener'和漸進增強)。無論哪種方式,你都不使用'onload'。 – Quentin