簡體   English   中英

單擊錨點以顯示下面隱藏的 div,然后隱藏所有其他內容 div?

[英]Click an anchor to reveal hidden div beneath, then hide all other content divs?

我創建了 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM