简体   繁体   English

如何使用javascript显示/隐藏多个div?

[英]How to show/hide plural divs with javascript?

Me and my collegues are working on a project where we make a website to show people our study.我和我的同事正在做一个项目,我们制作一个网站来向人们展示我们的研究。 We have 1 page with many descriptions on some subjects.我们有 1 页,其中包含有关某些主题的许多描述。 There are 6 in total.共有 6 个。 systeembiologie, proteomics, genomics, transcriptomics, epigenomics and metabolomics.系统生物学、蛋白质组学、基因组学、转录组学、表观基因组学和代谢组学。 All of these subjects are explained in a seperate div.所有这些主题都在单独的 div 中进行了解释。 Our page is really long right now and we would like to have a function when the page starts with htese divs hidden, then you can click on a any of these words and the corresponding div opens, if another of these words is clicked then the original div will close and the new one will open.我们的页面现在真的很长,我们希望有一个功能,当页面开始时隐藏htese div,然后您可以单击这些单词中的任何一个并打开相应的div,如果单击这些单词中的另一个则是原始的div 将关闭,新的将打开。 None of us have any experience in javascript and cant seem to figure out how to do this.我们中没有人在 javascript 方面有任何经验,似乎无法弄清楚如何做到这一点。 If anybody could help us, that would be amazing.如果有人能帮助我们,那就太棒了。

Here's a quick fix.这是一个快速修复。 I'm sure you'll be able to copy the other topics in HTML yourself.我相信您可以自己复制 HTML 中的其他主题。

 const links = [...document.getElementsByTagName("a")]; const topics = [...document.getElementsByClassName("topic")]; links.map((link) => link.addEventListener("click", function() { topics.map((topic) => topic.style.display = "none"); const selected = link.text; document.getElementById(selected).style.display = "inline-block"; }));
 * { box-sizing: border-box; } html { font-size: 62.5%; } a { text-decoration: none; color: black; } nav ul { list-style: none; padding: 20px; display: flex; align-content: center; justify-content: space-between; background-color: lightblue; font-size: 1.4rem; } .topic { display: none; }
 <div class="page"> <nav> <ul> <li><a href="#">systeembiologie</a></li> <li><a href="#">proteomics</a></li> <li><a href="#">genomics</a></li> <li><a href="#">transcriptomics</a></li> <li><a href="#">epigenomics</a></li> <li><a href="#">metabolomics</a></li> </ul> </nav> <div id="systeembiologie" class="topic"> <div class="description">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div> </div> <div id="proteomics" class="topic"> <div class="description">Viverra orci sagittis eu volutpat. Tellus in metus vulputate eu scelerisque felis. Tristique et egestas quis ipsum suspendisse ultrices. Vitae semper quis lectus nulla at volutpat. Commodo sed egestas egestas fringilla phasellus faucibus scelerisque eleifend donec. Urna condimentum mattis pellentesque id nibh. Ac tortor vitae purus faucibus ornare suspendisse. Gravida arcu ac tortor dignissim convallis. Platea dictumst vestibulum rhoncus est. Viverra maecenas accumsan lacus vel facilisis volutpat est velit egestas. Ultrices in iaculis nunc sed augue lacus.</div> </div> </div>

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

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