简体   繁体   English

javascript切换显示和隐藏多个div元素

[英]javascript toggle showing and hiding several div elements

I have two divs on a page (id's MAY and JUNE) and two buttons on the page. 我在页面上有两个div(id为MAY和JUNE),在页面上有两个按钮。 The page startes off by showing the May div, with the June div hidden using css display: none 该页面首先显示May div,使用CSS显示隐藏June div:无

I am trying to create the correct javascript that will toggle between the two of them, but (after searching on here) I can only manage to get one to work. 我正在尝试创建正确的JavaScript,以便在两者之间进行切换,但是(在这里搜索之后)我只能设法使它们起作用。

Codepen showing issue is https://codepen.io/billteale/pen/zwBBez Codepen显示问题是https://codepen.io/billteale/pen/zwBBez

<a href="#" id="button" >MAY</a>
<a href="#" id="button" >JUNE</a>
<!-- first div, shows on page load -->
<div id="MAY">
      <div style="background-color: lightgrey"><h1 style="padding: 5px"><strong>May 2017</strong></h1>
     </div>
<!-- second div, hidden originally -->
  <div class="hidden" id="JUNE"> 
      <div style="background-color: lightgrey"><h1 style="padding: 5px"><strong>June 2017</strong></h1>
      </div>

...and the current js is ...而目前的js是

var button = document.getElementById('button'); // Assumes element with id='button'
button.onclick = function() {
  var div = document.getElementById('MAY');
  if (div.style.display !== 'none') {
    div.style.display = 'none';
  }
  else {
    div.style.display = 'block';
  }
};

Eventually I will have four or more divs to toggle between, with each button showing its relevant div, and hiding the others. 最终,我将有四个或更多的div切换,每个按钮显示其相关的div,并隐藏其他div。

Can anybody tell me how to add to the code I have here to make this work for multiple elements? 谁能告诉我如何添加到这里的代码中,以使该功能适用​​于多个元素?

It can be done by setting the div id's in the anchor tag's href attribute, and showing the corresponding div while hiding the rest. 可以通过在锚标记的href属性中设置div ID并隐藏相应的div来完成。 It can be done for any number of div 's with no extra script, Just add the new div id to the new anchor tags href . 可以在不使用任何额外脚本的情况下对任意数量的div进行操作,只需将新的div ID添加到新的锚标签href You should give a common class to all the div 's like month to select them all. 您应该为所有div的like month提供一个通用类,以全部选择它们。

 $("a.btn").click(function() { var id = $(this).attr("href"); $("div.month:visible").hide(); $(id).show(); }); 
 .hidden { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#MAY" class="btn" id="button-may">MAY</a> <a href="#JUNE" class="btn" id="button-june">JUNE</a> <!-- first div, shows on page load --> <div id="MAY" class="month"> <div style="background-color: lightgrey"> <h1 style="padding: 5px"><strong>May 2017</strong></h1> </div> </div> <!-- second div, hidden originally --> <div class="month hidden" id="JUNE"> <div style="background-color: lightgrey"> <h1 style="padding: 5px"><strong>June 2017</strong></h1> </div> </div> 

 div.style.visibility = "hidden"

this will completely hide your div. 这将完全隐藏您的div。 I'm pretty sure this is what you are asking for 我很确定这是您要的

also instead of that you can make a separate function and add o'clock to div 除此之外,您还可以创建一个单独的函数,然后将clock添加到div

 <div onclick="nameoffunction()"></div>

the function can take in a parameter and each div on click function can have the parameter of its id 该函数可以接受一个参数,并且每个div单击功能可以具有其id的参数

Your div were messed up! 您的div搞砸了! The snippet is working now with the help of jQuery. 该代码段现在在jQuery的帮助下工作。

 $("#JUNE").hide(); $("#MAY").show(); $('#button-may').on("click", function() { $("#JUNE").hide(); $("#MAY").show(); }); $('#button-june').on("click", function() { $("#JUNE").show(); $("#MAY").hide(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" id="button-may">MAY</a> <a href="#" id="button-june">JUNE</a> <!-- first div, shows on page load --> <div id="MAY"> <div style="background-color: lightgrey"> <h1 style="padding: 5px"><strong>May 2017</strong></h1> </div> </div> <!-- second div, hidden originally --> <div class="hidden" id="JUNE"> <div style="background-color: lightgrey"> <h1 style="padding: 5px"><strong>June 2017</strong></h1> </div> </div> 

First you add a "div-toggle" class to each one of the divs you want to toggle. 首先,向要切换的每个div添加一个“ div-toggle”类。 After that, you bind click events on your buttons, firing a function which takes an identifier as argument. 之后,您将单击事件绑定到按钮上,从而触发一个将标识符作为参数的函数。

The function will run through your divs and set the one that has the argument id as visible, and hide the others. 该函数将遍历您的div,并将具有参数id的一个设置为可见,然后隐藏其他的。

This way you can add more divs to be toggled. 这样,您可以添加更多的div进行切换。 You just have to mark them with the "div-toggle" class, set their id's and create their respective buttons. 您只需要用“ div-toggle”类标记它们,设置它们的ID并创建它们各自的按钮即可。

 var divs = document.getElementsByClassName('div-toggle'); function toggle(id) { for (var i = 0; i < divs.length; i++) { var div = divs[i]; if (div.id == id) div.style.display = 'block'; else div.style.display = 'none'; } } 
 <a href="#" onclick="toggle('MAY')" >MAY</a> <a href="#" onclick="toggle('JUNE')" >JUNE</a> <!-- first div, shows on page load --> <div class="div-toggle" style="display:block;" id="MAY"> <div style="background-color: lightgrey"> <h1 style="padding: 5px"><strong>May 2017</strong></h1> </div> </div> <!-- second div, hidden originally --> <div class="div-toggle" style="display:none;" id="JUNE"> <div style="background-color: lightgrey"> <h1 style="padding: 5px"><strong>June 2017</strong></h1> </div> </div> 

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

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