简体   繁体   中英

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. 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.

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.

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.

 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.

 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.

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');
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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