javascript - 单击 anchor 以显示下面隐藏的 div,然后隐藏所有其他内容 div?

标签 javascript jquery html css

我创建了 4 个容器,单击它们可以显示下面的内容。我想一次只显示一个隐藏的 div。因此,如果用户单击另一个容器,则所有其他容器都会被隐藏。当前,一旦单击它们,我就无法隐藏任何容器。我真的很感谢任何帮助:)

JS

function toggle_visibility(id) {
  var e = document.getElementById(id);
  if(e.style.display == 'none')
     e.style.display = 'block';
  else
     e.style.display = 'none';
}

CSS

html, body { height: 100%; padding: 0 ; margin: 0; }
a { width: 100%; height: 100%; color: #000; display: block; position: absolute; background: #647070; }
.section { width: 49.9%; height: 49.9%; float: left; position: relative; overflow: hidden; }
#div1, #div3 { border-right: 1px solid black; }
#div3, #div4 { border-top: 1px solid black; }

HTML

  <div id="div1" class="section">
    <div id="festival">
      <a href="#" onclick="toggle_visibility('festival');" style="">Festival&trade;</a>
    </div>
     <p>This is the content of Q1</p>
  </div>

  <div id="div2" class="section">
    <div id="register">
      <a href="#" onclick="toggle_visibility('register');" style="">Register</a>
    </div>
    <p>This is the content of Q2</p>
  </div>

  <div id="div3" class="section">
    <div id="connect">
      <a href="#" onclick="toggle_visibility('connect');" style="">Connect</a>
    </div>
    <p>This is the Q3 content.</p>
  </div>

  <div id="div4" class="section">
    <div id="forth">
      <a href="#" onclick="toggle_visibility('forth');" style="">Forth</a>
    </div>
    <p>This is the Q4 content.</p>
  </div> 

最佳答案

你可以添加这样的东西:

var divsToHide = document.querySelectorAll(".section > div")
for (var i = 0; i < divsToHide.length; i++) {
  divsToHide[i].style.display = "block";
}

这将遍历每个 .section 并显示它的直接 div。

演示

function toggle_visibility(id) {

  var divsToHide = document.querySelectorAll(".section > div")
  for (var i = 0; i < divsToHide.length; i++) {
    divsToHide[i].style.display = "block";
  }
  var e = document.getElementById(id);
  if (e.style.display == 'none')
    e.style.display = 'block';
  else
    e.style.display = 'none';
}
html,
body {
  height: 100%;
  padding: 0;
  margin: 0;
}

a {
  width: 100%;
  height: 100%;
  color: #000;
  display: block;
  position: absolute;
  background: #647070;
}

.section {
  width: 49.9%;
  height: 49.9%;
  float: left;
  position: relative;
  overflow: hidden;
}

#div1,
#div3 {
  border-right: 1px solid black;
}

#div3,
#div4 {
  border-top: 1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="div1" class="section">
  <div id="festival">
    <a href="#" onclick="toggle_visibility('festival');" style="">Festival&trade;</a>
  </div>
  <p>This is the content of Q1</p>
</div>

<div id="div2" class="section">
  <div id="register">
    <a href="#" onclick="toggle_visibility('register');" style="">Register</a>
  </div>
  <p>This is the content of Q2</p>
</div>

<div id="div3" class="section">
  <div id="connect">
    <a href="#" onclick="toggle_visibility('connect');" style="">Connect</a>
  </div>
  <p>This is the Q3 content.</p>
</div>

<div id="div4" class="section">
  <div id="forth">
    <a href="#" onclick="toggle_visibility('forth');" style="">Forth</a>
  </div>
  <p>This is the Q4 content.</p>
</div>

关于javascript - 单击 anchor 以显示下面隐藏的 div,然后隐藏所有其他内容 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58063783/

相关文章:

javascript - .append()、prepend()、.after() 和 .before()

javascript - 添加元素的进度条

Javascript 太慢?

jquery - Kendo UI 工具提示显示,访问目标?

javascript - javascript和html中xss编码的区别

html - 隐藏的溢出在 CSS 网格图像中不起作用

javascript - 我需要帮助使用 jquery 创建 4 个可折叠面板

html - 为什么悬停时所有图像都会抖动

javascript - 向下滚动时替换固定图像,然后将其更改为相对图像

javascript - 如何使用 angular.copy() 保留原型(prototype)?