小编Par*_*ode的帖子

在 HTML 中隐藏和显示 Div 标签

在这里,我试图隐藏该块,但第一次我需要在每次刷新页面时双击该按钮。我在下面附上了我的代码以供参考。

<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    #myDIV {
      width: 100%;
      padding: 50px 0;
      text-align: center;
      background-color: lightblue;
      margin-top: 20px;
      display: none;
    }
  </style>
</head>

<body>

  <p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>
  <button onclick="myFunction()">Try it</button>
  <div id="myDIV">
    This is my DIV element.
  </div>

  <p><b>Note:</b> The element will not take up any space when the display property set to "none".</p>
  <script>
    function myFunction() {
      var x = document.getElementById("myDIV");
      if (x.style.display …
Run Code Online (Sandbox Code Playgroud)

html javascript css

6
推荐指数
1
解决办法
457
查看次数

标签 统计

css ×1

html ×1

javascript ×1