我希望在一开始时隐藏文本,点击按钮后才能显示。如果有人能找出我的代码中的错误,我将非常感激。
function F1()
{
var x = document.getElementById("step1DIV");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
<!DOCTYPE html>
<html>
<body>
<button onclick="F1()"> <b>Step 1</b> </button>
<div id="step1DIV">
<p> text </p>
</div>
</body>
</html>
style.display
是不可用的。我建议在 CSS 中使用.hide{ display:none; }
,然后使用Element.classList.add('hide')
、Element.classList.remove('hide')
或Element.classList.toggle('hide')
。除非你想要大量输入,否则我通常建议使用 CSS 类来进行样式设置。 - StackSlave