2014-10-04 91 views
0

我試圖做一些基於文本的遊戲,但使用按鈕而不是文本輸入。例如,我想從一個頁面上的3個按鈕開始。當你選擇其中一個按鈕時,函數應該改變這個問題變量,然後爲下一個問題創建按鈕。按鈕,改變一個變量,並創建一個新的按鈕

問題一:

<DOCTYPE html> 
<body> 
<button onclick="russetPotato()">Russet</button> 
<button onclick="redPotato()">Red</button> 
<button onclick="sweetPotato()">Sweet</button> 
</body> 
<script type="text/javascript"> 
var seed = 0; 
var soil = 0; 
function russetPotato(){ 
    seed = 1 
    document.write("You set seed as Russet Potato."); 
} 
function redPotato(){ 
    seed = 2 
    document.write("You set seed as Red Potato."); 
} 
function sweetPotato(){ 
    soil = 3 
    document.write("You set seed as Sweet Potato."); 
} 
</script> 
</html> 

現在我想要做的就是添加的第二個問題。在種子按鈕被回答後,我可以如何讓土壤按鈕出現,而不是同時出現?

<DOCTYPE html> 
    <body> 
    <button onclick="russetPotato()">Russet</button> 
    <button onclick="redPotato()">Red</button> 
    <button onclick="sweetPotato()">Sweet</button> 
    <button onclick="sandySoil()">Thin Sandy Soil</button> 
    <button onclick="loamSoil()">Loose Loam Soil</button> 
    <button onclick="claySoil()">Heavy Clay Soil</button> 
    </body> 
    <script type="text/javascript"> 
    var seed = 0; 
    var soil = 0; 
    function russetPotato(){ 
     seed = 1 
     document.write("You set seed as Russet Potato."); 
    } 
    function redPotato(){ 
     seed = 2 
     document.write("You set seed as Red Potato."); 
    } 
    function sweetPotato(){ 
     soil = 1 
     document.write("You set seed as Sweet Potato."); 
    } 
    function sandySoil(){ 
     soil = 1 
     document.write("You set soil as Thin Sandy Soil."); 
    } 
    function loamSoil(){ 
     soil = 2 
     document.write("You set soil as Loose Loam Soil."); 
    } 
    function claySoil(){ 
     soil = 3 
     document.write("You set soil as Heavy Clay Soil."); 
    } 
    </script> 
    </html> 

道歉,如果這其實很簡單。這是我第一次使用javascript/html進行實驗。

+1

您不能在結尾處的''標籤下面'document.write'。反正你不應該使用'docement.write',因爲它不能與XHTML一起使用,並且你想要可重用的代碼。真正的使用應該在''中使用外部JavaScript,並讓你的代碼執行'onload',因爲'

相關問題