简体   繁体   English

如何在Javascript中获取和隐藏兄弟姐妹

[英]How to get and hide siblings in Javascript

<ul>
    <li><a>home</li>
    <li><a onclick="erm()">about</li>
    <li><a>contact</li>
</ul>

<div id="all">
    <div id="home">Home</div>
    <div id="about">About</div>
    <div id="contact">Contact</div>
</div>

The divs from #all will initially be hidden. #all中的div最初将被隐藏。

How would I show just the contents of the #about div if the user will click on the 'about link' and hide the siblings in the same time? 如果用户单击“关于链接”并同时隐藏兄弟姐妹,我将如何仅显示#about div的内容?

I will basically use the same function on the same event for the other links. 对于其他链接,我将基本上在相同事件上使用相同的功能。

Even a pseudo code might help, I can't get my head around it. 即使是伪代码也可能有所帮助,我无法理解。

no jQuery please, but vanilla Javascript . 请不要jQuery,但要使用Javascript

A solution : 一个解法 :

function hideAllChildrenButOne(parentId, toRevealId) {
     var children = document.getElementById(parentId).children;
     for (var i=0; i<children.length; i++) children[i].style.display="none";
     document.getElementById(toRevealId).style.display="block";
}
hideAllChildrenButOne('all', 'about');

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

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