简体   繁体   English

显示/隐藏具有链接的多个Div

[英]Show/hide multiple Divs with links

I didn't clearly explain what I was trying to do. 我没有明确解释我要做什么。 Hopefully this will be better. 希望这会更好。

Currently I'm using this Fiddle to toggle some divs. 目前,我正在使用此Fiddle切换某些div。 It acts as an accordion and shows only one div at a time. 它充当手风琴,一次仅显示一个格。 Clicking one of the titles will show the content of that div and clicking another title will hide the first div and show that one. 单击一个标题将显示该div的内容,单击另一个标题将隐藏第一个div并显示该div。

What I am having trouble with (and would like to do) is when opening one div I would like to hide access to the other divs until that first div is closed. 我遇到的麻烦(并且想做)是在打开一个div时,我想隐藏对其他div的访问权限,直到第一个div关闭。

Meaning if I Click on "Content2" to show that content, I would like to hide access to Content1, Content3, and Content4 until Content 2 is closed again. 这意味着如果我单击“ Content2”以显示该内容,我想隐藏对Content1,Content3和Content4的访问,直到再次关闭Content 2。

 function ReverseDisplay(d) { var els = document.querySelectorAll('.toggle.active:not(#' + d + ')'); for (var i = 0; i < els.length; i++) { els[i].classList.remove('active'); } document.getElementById(d).classList.toggle('active') } 
 .toggle { display: none; } .toggle.active { display: block; } 
 <a href="javascript:ReverseDisplay('content1')"> Content1 </a> <a href="javascript:ReverseDisplay('content2')"> Content2 </a> <a href="javascript:ReverseDisplay('content3')"> Content3 </a> <a href="javascript:ReverseDisplay('content4')"> Content4 </a> <div id="content1" class="toggle"> <p>Content 1 goes here.</p> </div> <div id="content2" class="toggle"> <p>Content 2 goes here.</p> </div> <div id="content3" class="toggle"> <p>Content 3 goes here.</p> </div> <div id="content4" class="toggle"> <p>Content 4 goes here.</p> </div> 

This should work... Use the id passed to identify the element and add class by checking if active exists. 这应该起作用...使用传递的id来标识元素,并通过检查是否存在active添加类。

 function ReverseDisplay(d) { var id = d var el = document.getElementById(id) var elClassList = el.classList var [...active] = document.querySelectorAll('.toggle.active') debugger if (active.length === 0) { el.classList.add('active') } else if (id === active[0].id) { el.classList.remove('active') } } 
 .toggle { display: none; margin-top: 40px; } .toggle.active { display: block; } a { position: fixed; top: 10px; } 
 <a href="javascript:ReverseDisplay('uniquename')"> Click to show/hide. </a> <div id="uniquename" class="toggle"> <p>Content 1 goes here.</p> </div> <a href="javascript:ReverseDisplay('uniquename1')" style="left:150px"> Click to show/hide. </a> <div id="uniquename1" class="toggle"> <p>Content 2 goes here.</p> </div> 

Use this javascript function in order to achieve accordion style behaviour, for as many links as you might want to have. 使用此javascript函数可实现手风琴风格的行为,并获得尽可能多的链接。

function ReverseDisplay(d) {
  var els = document.getElementById(d);
  if (els.classList.contains('active')){
    els.classList.remove('active');
  }else{
    var activeDivs = document.getElementsByClassName('active');
    for (var i = 0; i < activeDivs.length; i++) {
        activeDivs[i].classList.remove('active');
        }
    els.classList.add('active');
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM