简体   繁体   English

切换多个div

[英]Toggle multiple divs

Suppose I created 7 anchor tags. 假设我创建了7个定位标记。 I named them ABOUT, STAFF, VOLUNTEER, FINANCE, SPONSORS, SUBMIT FILM and WINNERS. 我给他们取名为ABOUT,STAFF,VOLUNTEER,FINANCE,SPONSORS,SUBMIT FILM和WINNERS。 Also, suppose that I created 7 containing content like text and images. 另外,假设我创建了7个包含文本和图像之类的内容。 By default, I hid all 7 . 默认情况下,我将所有7隐藏。 Only the anchor tags are showing one after the other like below: 仅锚标记显示一个接一个,如下所示:

[one] [two] [three] [four] [five] [six] [seven] [一二三四五六七]

All anchor tags are lined up horizontally next to one another with white backgrounds, framed in black, 1 pixel wide and the font also in black, using CSS called "sub-menu." 使用称为“子菜单”的CSS,将所有锚点标记彼此水平排列,并排成一排,底色为白色,黑色,1像素宽,字体也为黑色。 Easy. 简单。 No problem. 没问题。

Suppose that I gave each a distinct ID (eg, id="aboutsec", id="staffsec", id="volunteersec", etc.)? 假设我给每个ID赋予了唯一的ID(例如,id =“ aboutsec”,id =“ staffsec”,id =“ volunteersec”等)?

Using snippets of JavaScript found online, I managed to show my hidden and hide them when the same anchor tag is clicked. 使用在线找到的JavaScript片段,我设法显示了隐藏的内容,并在单击相同的锚标记时将它们隐藏了。 For example, when I click on the anchor tag titled "About" it shows , and when I click on the exact same anchor, it hides it. 例如,当我单击标题为“关于”的锚标记时,它显示,而当我单击完全相同的锚时,它将隐藏它。

That part was easy. 那部分很简单。

However, suppose that when I click anchor About, showing I want to hide when I click on anchor Staff or Volunteer or Finance or Sponsors, etc? 但是,假设当我单击锚点“关于”时,当我单击锚点“职员”或“志愿者”,“财务”或“赞助者”等时,我想隐藏吗? In other words, a toggle? 换句话说,切换?

What I am trying to achieve is that when any of the anchors are clicked, showing its corresponding , when a different anchor is clicked, I want it to hide the others, which ever is currently showing. 我想要实现的是,当单击任何锚点时,显示其对应的,当单击其他锚点时,我希望它隐藏其他当前显示的锚点。 Make sense? 说得通?

These are the anchor tags that I am currently using: 这些是我当前正在使用的定位标记:

    <a href="javascript:void()" class="sub-menu" 
    onclick="showme()">About</a>
    <a href="javascript:void()" class="sub-menu" 
    onclick="showme2()">Staff</a>
    <a href="javascript:void()" class="sub-menu" 
    onclick="showme3()">Volunteer</a>
    <a href="javascript:void()" class="sub-menu" 
    onclick="showme4()">Finance</a>
    <a href="javascript:void()" class="sub-menu" 
    onclick="showme5()">Sponsors</a>
    <a href="javascript:void()" class="sub-menu" 
    onclick="showme6()">Submit Film</a>
    <a href="javascript:void()" class="sub-menu" 
    onclick="showme7()">Winners</a>

The are titled using IDs: 的标题使用ID:

    <section id="aboutsec" class="subsec" style="display:none">

I have an image at the bottom of each that is wrapped inside an anchor tag that uses the same onclick function that effectively hides the that is showing. 我在每个图像的底部都有一个图像,该图像包裹在锚标签中,该标签使用相同的onclick函数有效隐藏了所显示的图像。 This part works just fine. 这部分工作正常。 So, what I have is an show/hide situation. 所以,我所看到的是一个隐藏的情况。 Great, but what if someone clicks on "Staff" or "Volunteer" or any other anchor? 太好了,但是如果有人点击“职员”或“志愿者”或任何其他锚点怎么办? It shows another but does not hide the open ones. 它显示另一个,但不隐藏打开的。

This is the JS that I am using, and I've had to repeat each to show and hide each : 这是我正在使用的JS,我不得不重复每个JS来显示和隐藏每个JS:

    <!--Show and hide sub-section About-->
    <script>
    function showme() {
    var x = document.getElementById("aboutsec");
    if (x.style.display === "none") {
    x.style.display = "block";
    } else {
    x.style.display = "none";
    }
    }
    </script>

    <!--Show and hide sub-section Staff-->
    <script>
    function showme2() {
    var x = document.getElementById("staffsec");
    if (x.style.display === "none") {
    x.style.display = "block";
    } else {
    x.style.display = "none";
    }
    }
    </script>

    <!--Show and hide sub-section Volunteer-->
    <script>
    function showme3() {
    var x = document.getElementById("volunteersec");
    if (x.style.display === "none") {
    x.style.display = "block";
    } else {
    x.style.display = "none";
    }
    }
    </script>

You can use anchor tags using href as id: 您可以将href用作id来使用锚标记:

    <a href="#aboutsec" class="sub-menu" 
    onclick="showme(this)">About</a>
    <a href="#staffsec" class="sub-menu" 
    onclick="showme(this)">Staff</a>

Then use it to get the actual element needed: 然后使用它来获取所需的实际元素:

    <script>
    function showme(tag) {
      var x = document.querySelector(tag.href);
      if (x.style.display === "none") {
        x.style.display = "block";
      } else {
        x.style.display = "none";
      }
    }
    </script>

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

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