簡體   English   中英

單擊按鈕一定次數后,如何使按鈕做些不同的事情?

[英]How do you make a button do something different after a certain number of times of clicking it?

我正在編寫文本冒險游戲,但已經有一段時間沒有使用Java腳本了,我希望代碼中的“洞窟感覺”按鈕第二次單擊它就轉到“ d”。 我該怎么做呢? 這是我當前的代碼:

<script>
function one()
{
    var newButton1 = '<button id="btnTwo" onclick="two()" >Pick up stick</button>';
    var newButton2 = '<button id="btnThree" onclick="three()">Leave it there</button>';
    document.getElementById("a").innerHTML="You feel something on the ground, and you think it's a stick."+newButton1+newButton2;
}

function two()
{
   document.getElementById("b").innerHTML="You pick up the stick. It might be useful for something."; 

   document.getElementById("btnTwo").style.display = 'none';
   document.getElementById("btnThree").style.display = 'none';

}

function three()
{
    document.getElementById("c").innerHTML="You leave the stick on the ground and continue on.";

    document.getElementById("btnTwo").style.display = 'none';
    document.getElementById("btnThree").style.display = 'none';
}

function four()
{
    document.getElementById("d").innerHTML="You find another stick stuck to the wall with something that feels like honey. You can also feel some rocks stuck to the wall next to it.";
}
</script>

<div style="margin-left:15px; width:200px; margin-top:100px;">
    <button onclick="one()">Feel around the cave</button>
</div>

<div id="entire" style="margin-left:490px; margin-top:-22px; width:400px; height:600px;>
   <div id="d"></div>
   <div id="c"></div>
   <div id="b"></div>
   <div id="a"></div>
</div>

我不會把onclick =“”弄亂,但是既然你這樣做了,那就

給按鈕分配id,讓說id =“ btn_feelAround”然后在one()函數的末尾執行

var myButton = document.getElementById('btn_feelAround');
myButton.onclick = four;

我在這里做的: jsFiddle

另外,嘗試學習jquery或類似的東西,它可能會讓您更輕松

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM