简体   繁体   中英

How could I make sure that at least one DIV is always shown?

I have this piece of code I found that will show or hide a DIV using javascript. It only allows one div to be shown which is what I want. However, I want it where at least one DIV is always shown at a time. It basically toggles between hiding and showing them currently. I want it to be where if i hit DIV two again, it wont hide it and it will do nothing. Please help, thank you.

page.html

    <script src="scripts/pages.js"></script>
    <div class="main_div">
        <div class="inner_div">
            <div id="Div1">I'm Div One</div>
            <div id="Div2" style="display: none;">I'm Div Two</div>
            <div id="Div3" style="display: none;">I'm Div Three</div>
            <div id="Div4" style="display: none;">I'm Div Four</div>
        </div>
        <div class="buttons">
            <a href="#" onclick="divVisibility('Div1');">Div1</a> | 
            <a href="#" onclick="divVisibility('Div2');">Div2</a> | 
            <a href="#" onclick="divVisibility('Div3');">Div3</a> | 
            <a href="#" onclick="divVisibility('Div4');">Div4</a>
        </div>
    </div>

pages.js

var divs = ["Div1", "Div2", "Div3", "Div4"];
var visibleDivId = null;
function divVisibility(divId) {
  if(visibleDivId === divId) {
    visibleDivId = null;
  } else {
    visibleDivId = divId;
  }
  hideNonVisibleDivs();
}
function hideNonVisibleDivs() {
  var i, divId, div;
  for(i = 0; i < divs.length; i++) {
    divId = divs[i];
    div = document.getElementById(divId);
    if(visibleDivId === divId) {
      div.style.display = "block";
    } else {
      div.style.display = "none";
    }
  }
}

You can simplify your code to following. Where you hide all the divs and show the div corresponding to the clicked element.

 function divVisibility(divId) { document.querySelectorAll(".inner_div > div").forEach(e => e.style.display = 'none'); document.getElementById(divId).style.display = 'block'; } 
 <div class="main_div"> <div class="inner_div"> <div id="Div1">I'm Div One</div> <div id="Div2" style="display: none;">I'm Div Two</div> <div id="Div3" style="display: none;">I'm Div Three</div> <div id="Div4" style="display: none;">I'm Div Four</div> </div> <div class="buttons"> <a href="#" onclick="divVisibility('Div1');">Div1</a> | <a href="#" onclick="divVisibility('Div2');">Div2</a> | <a href="#" onclick="divVisibility('Div3');">Div3</a> | <a href="#" onclick="divVisibility('Div4');">Div4</a> </div> </div> 

The part that causes this function to hide a div is the setting of visibleDivId to null.

function divVisibility(divId) {
  if(visibleDivId === divId) {
    // visibleDivId = null;
  } else {
    visibleDivId = divId;
  }
  hideNonVisibleDivs();
}

Commenting that line out (see line 3) keeps the clicked div as the visibleDivId and the second function (hideNonVisibleDivs) won't hide it.

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