在点击一定次数后,如何使按钮做一些不同的事情?

时间:2013-11-29 19:50:34

标签: javascript

我正在编写一个文本冒险代码并且我有一段时间没有完成Javascript,我希望我的代码中的“感觉在洞穴周围”按钮在第二次点击它时转到“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>

1 个答案:

答案 0 :(得分:0)

我不会乱用onclick =&#34;&#34;但既然你这样做了,那就

将id分配给按钮,让我们说id =&#34; btn_feelAround&#34;然后在one()函数的末尾做

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

我在这里做了:jsFiddle

此外,尝试学习jquery或类似的东西,它可能会让你更容易

相关问题