如何使一个按钮在单击时显示另一个按钮

How to make a button show another button onclick?

本文关键字:按钮 显示 另一个 单击 何使一      更新时间:2023-12-10

我正在尝试编写一个文本冒险,由于我大部分时间都是用PHP而不是Javascript进行编码,我想我忘记了Javascript的一切:p

无论如何,我正在寻找如何在单击另一个按钮后显示新按钮。我在"innerHTML"中有文本,所以我不知道如何使用它显示另一个按钮。这是我到目前为止的代码(当你点击新按钮时,它应该指向"b")

<script>
function one()
{
document.getElementById("a").innerHTML="You feel something on the ground, and you think it's a stick.";
}
function two()
{
document.getElementById("b").innerHTML="You pick up the stick. It might be useful for something.";
}
</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="b"></div><div id="a"></div></div>

在页面末尾,在脚本标记中添加以下代码

var a = document.getElementById('a');
a.addEventListener('click', function () { two()});

您应该查看jQuery

尝试将其添加到您的one()函数中:

var newButton = '<button onclick="two()">Pick up stick</button>';
document.getElementById("a").innerHTML="You feel something on the ground, and you think it's a stick."+newButton;

这将把按钮放在"a"分区内。