小编Mic*_*ine的帖子

显示/隐藏多个Divs Javascript

寻找一个好的JavaScript,以帮助我隐藏/显示多个div与一个按钮点击而不是一个点击,所以我可以在博客中使用它.我一直在寻找一段时间的答案,并且无法找到一个使用JavaScript和/或CSS的好答案.我是一个新手所以请耐心等待.以下是我的代码,但我想简化它并使其工作,以便当我再次单击相应的按钮时它将关闭div.

CSS

    <head>
    <style>
    #myDIV1 {
      width: 150px;
      height: 150px;
      background-color: lightblue;
      display: none;
    }


    #myDIV2 {
      width: 150px;
      height: 150px;
      background-color: lightblue;
      display: none;
    }


    #myDIV3 {
      width: 150px;
      height: 150px;
      background-color: lightblue;
      display: none;
    }


    #myDIV4 {
      width: 150px;
      height: 150px;
      background-color: lightblue;
      display: none;
    }

    </style>
    </head>
Run Code Online (Sandbox Code Playgroud)

我知道有一种更简单的方法,但这是我能找到的唯一方法,它适用于我希望它在大多数情况下做的事情

HTML

    <body>

      <p>Click button to see div.</p>

      <button onclick="myFunction1()">One</button>

      <button onclick="myFunction2()">Two</button>

      <button onclick="myFunction3()">Three</button>

      <button onclick="myFunction4()">Four</button>

    <div id="myDIV1">

      This is the div1 element.

    </div>

    <div id="myDIV2">

      This is the div2 …
Run Code Online (Sandbox Code Playgroud)

html

5
推荐指数
2
解决办法
2万
查看次数

标签 统计

html ×1