Javascript 单击一个按钮时,如何隐藏两个按钮?

Javascript 单击一个按钮时,如何隐藏两个按钮?,javascript,button,hide,Javascript,Button,Hide,在我的文字冒险游戏中,有两种选择-1。拿起棍子或2。放在那儿吧。您只能选择一个按钮,因此我想知道如何在单击一个按钮后隐藏这两个按钮。这是当前代码: <script> function one() { var newButton1 = '<button onclick="two()">Pick up stick</button>'; var newButton2 = '<button onclick="three()">Leave it there&

在我的文字冒险游戏中,有两种选择-1。拿起棍子或2。放在那儿吧。您只能选择一个按钮,因此我想知道如何在单击一个按钮后隐藏这两个按钮。这是当前代码:

<script>
function one()
{
var newButton1 = '<button onclick="two()">Pick up stick</button>'; var newButton2 = '<button 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."; 
}

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

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

<div style="margin-left:255px; width:200px; margin-top:-15px;">
</div>

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

功能一()
{
var newButton1='拿起棍子';var newButton2='把它留在那里';
document.getElementById(“a”).innerHTML=“你感觉到地面上有东西,你认为它是一根棍子。”+newButton1+newButton2;
}
函数二()
{
document.getElementById(“b”).innerHTML=“拿起棍子。它可能对某些事情有用。”;
}
职能三()
{
document.getElementById(“c”).innerHTML=“将棍子留在地上,然后继续。”;
}
摸摸洞穴

您可以指定按钮ID,然后将其显示属性设置为隐藏:

function hideButtons()
{
  document.getElementById("pickUpStick").style.display = 'none';
  document.getElementById("leaveStick").style.display = 'none';
}

function name()
{
  ...
  hideButtons();
}

<button id="pickUpStick" ... 
<button id="leaveStick" ... 
函数隐藏按钮()
{
document.getElementById(“pickUpStick”).style.display='none';
document.getElementById(“leveStick”).style.display='none';
}
函数名()
{
...
隐藏按钮();
}

你需要给这些按钮一个id,当你点击其中一个按钮时,你会同时隐藏它们

看到它在这里工作:

守则:

<script>
function one()
{
//create buttons and give id to it (btnTwo and btnThree)
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."; 

    //Clicked, hide both buttons
    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.";
    //Clicked, hide both buttons
    document.getElementById("btnTwo").style.display = 'none';
    document.getElementById("btnThree").style.display = 'none';
}
</script>

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

<div style="margin-left:255px; width:200px; margin-top:-15px;">
</div>

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

功能一()
{
//创建按钮并为其提供id(btnTwo和btntree)
var newButton1=‘捡拾木棍’;
var newButton2='把它留在那里';
document.getElementById(“a”).innerHTML=“你感觉到地面上有东西,你认为它是一根棍子。”+newButton1+newButton2;
}
函数二()
{
document.getElementById(“b”).innerHTML=“拿起棍子。它可能对某些事情有用。”;
//单击,隐藏两个按钮
document.getElementById(“btnTwo”).style.display='none';
document.getElementById(“btntTree”).style.display='none';
}
职能三()
{
document.getElementById(“c”).innerHTML=“将棍子留在地上,然后继续。”;
//单击,隐藏两个按钮
document.getElementById(“btnTwo”).style.display='none';
document.getElementById(“btntTree”).style.display='none';
}
摸摸洞穴

同样,作为人们已经提到的内容的补充。您可以使用jQuery在一行中轻松完成这项工作。为两个按钮指定相同的类名,“按钮隐藏”或类似的名称。然后,在删除按钮功能中调用:

$(".button-hide").attr('style', 'display: none');

这里有一个更大的可伸缩性问题。您需要一个公共函数,它通过传递参数为您实现这一点,而不是为每个决策重复几乎相同的代码。