如何在Java中隐藏元素

问题描述 投票:1回答:3

我希望文本ist隐藏在开头,并在单击按钮后将其显示。如果有人在我的代码中发现错误,我将非常高兴。

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>
javascript html hide show
3个回答
1
投票

您需要给它一个初始样式,将其隐藏在HTML中。

function F1()
{
  var x = document.getElementById("step1DIV");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
  <button onclick="F1()"> <b>Step 1</b> </button>
  <div id="step1DIV" style="display: none;">
    <p> text </p>
  </div>

但是内联样式的设计较差,最好将类与CSS一起使用。

function F1()
{
  var x = document.getElementById("step1DIV");
  x.classList.toggle("hidden");
}
.hidden {
  display: none;
}
<button onclick="F1()"> <b>Step 1</b> </button>
  <div id="step1DIV" class="hidden">
    <p> text </p>
  </div>

0
投票

我只是将其定义为“无”,因此:

<div id="step1DIV" style="display: none"> 

0
投票

尝试将第一步DIV的display初始设置为none。使用内联样式或CSS。

© www.soinside.com 2019 - 2024. All rights reserved.