简体   繁体   English

显示/隐藏导航栏元素

[英]Show/Hide nav bar elements

I want to click on an element to show and when I click on another one the first one to hide.我想点击一个元素来显示,当我点击另一个元素时,第一个隐藏。 I want to do this with 4 elements but this only works when I'm doing this in an order, as soon as I skip a button the elements just stack on top of each other without hiding.我想用 4 个元素执行此操作,但这仅在我按顺序执行此操作时才有效,只要我跳过一个按钮,这些元素就会堆叠在一起而不会隐藏。

Some code:一些代码:

 function showHomePage() { document.getElementById("home").style.display = "block"; } function showSkillsPage() { document.getElementById("home").style.display = "none"; document.getElementById("skills").style.display = "block"; } function showProjectsPage() { document.getElementById("skills").style.display = "none"; document.getElementById("projects").style.display = "block"; } function showLanguagesPage() { document.getElementById("projects").style.display = "none"; document.getElementById("languages").style.display = "block"; } showHomePage();
 #home, #skills, #projects, #languages { display: none; }
 <ul id="top-menu-bar"> <li><a href="#" onclick="showHomePage()" >Home</a></li> <li><a href="#" onclick="showSkillsPage()" >Skills</a></li> <li><a href="#" onclick="showProjectsPage()" >Projects</a></li> <li><a href="#" onclick="showLanguagesPage()" >Languages</a></li> </ul> <p id="home">I'm the home page.</p> <p id="skills">I'm the skills page.</p> <p id="projects">I'm the projects page.</p> <p id="languages">I'm the languages page.</p>

You're only setting the display property of some items to none when you should be setting all the ones you don't want to see.当您应该设置所有您不想看到的项目时,您只是将某些项目的display属性设置为none

The items are stacking when you go out of order because your show...() functions are only setting the display property of the item before them.当您乱序时,这些项目正在堆叠,因为您的show...()函数仅设置它们之前项目的display属性。

Here's a sample which hides all items except the one you clicked:这是一个示例,它隐藏了除您单击的项目之外的所有项目:

 function showHomePage() { document.getElementById("home").style.display = "block"; document.getElementById("skills").style.display = "none"; document.getElementById("projects").style.display = "none"; document.getElementById("languages").style.display = "none"; } function showSkillsPage() { document.getElementById("home").style.display = "none"; document.getElementById("skills").style.display = "block"; document.getElementById("projects").style.display = "none"; document.getElementById("languages").style.display = "none"; } function showProjectsPage() { document.getElementById("home").style.display = "none"; document.getElementById("skills").style.display = "none"; document.getElementById("projects").style.display = "block"; document.getElementById("languages").style.display = "none"; } function showLanguagesPage() { document.getElementById("home").style.display = "none"; document.getElementById("skills").style.display = "none"; document.getElementById("projects").style.display = "none"; document.getElementById("languages").style.display = "block"; } showHomePage();
 #home, #skills, #projects, #languages { display: none; }
 <ul id="top-menu-bar"> <li><a href="#" onclick="showHomePage()" >Home</a></li> <li><a href="#" onclick="showSkillsPage()" >Skills</a></li> <li><a href="#" onclick="showProjectsPage()" >Projects</a></li> <li><a href="#" onclick="showLanguagesPage()" >Languages</a></li> </ul> <p id="home">I'm the home page.</p> <p id="skills">I'm the skills page.</p> <p id="projects">I'm the projects page.</p> <p id="languages">I'm the languages page.</p>


That fixes the issue, but it's still very repetitive.这解决了问题,但它仍然非常重复。 You can clean it up a little bit by introducing const reference variables for your items.您可以通过为您的项目引入const引用变量来稍微清理一下。 This will also help a little with performance since you aren't searching the DOM for them each time you click an item.这也会对性能有所帮助,因为您每次单击项目时都不会在 DOM 中搜索它们。

 const home = document.getElementById("home"); const skills = document.getElementById("skills"); const projects = document.getElementById("projects"); const languages = document.getElementById("languages"); function showHomePage() { home.style.display = "block"; skills.style.display = "none"; projects.style.display = "none"; languages.style.display = "none"; } function showSkillsPage() { home.style.display = "none"; skills.style.display = "block"; projects.style.display = "none"; languages.style.display = "none"; } function showProjectsPage() { home.style.display = "none"; skills.style.display = "none"; projects.style.display = "block"; languages.style.display = "none"; } function showLanguagesPage() { home.style.display = "none"; skills.style.display = "none"; projects.style.display = "none"; languages.style.display = "block"; } showHomePage();
 #home, #skills, #projects, #languages { display: none; }
 <ul id="top-menu-bar"> <li><a href="#" onclick="showHomePage()" >Home</a></li> <li><a href="#" onclick="showSkillsPage()" >Skills</a></li> <li><a href="#" onclick="showProjectsPage()" >Projects</a></li> <li><a href="#" onclick="showLanguagesPage()" >Languages</a></li> </ul> <p id="home">I'm the home page.</p> <p id="skills">I'm the skills page.</p> <p id="projects">I'm the projects page.</p> <p id="languages">I'm the languages page.</p>


You can clean it up even more with the realization that you could hide all the items then show the one you want.您可以进一步清理它,并意识到您可以隐藏所有项目然后显示您想要的项目。

 const all = document.querySelectorAll("#home, #skills, #projects, #languages"); const home = document.getElementById("home"); const skills = document.getElementById("skills"); const projects = document.getElementById("projects"); const languages = document.getElementById("languages"); function showHomePage() { all.forEach(item => item.style.display = "none"); home.style.display = "block"; } function showSkillsPage() { all.forEach(item => item.style.display = "none"); skills.style.display = "block"; } function showProjectsPage() { all.forEach(item => item.style.display = "none"); projects.style.display = "block"; } function showLanguagesPage() { all.forEach(item => item.style.display = "none"); languages.style.display = "block"; } showHomePage();
 #home, #skills, #projects, #languages { display: none; }
 <ul id="top-menu-bar"> <li><a href="#" onclick="showHomePage()" >Home</a></li> <li><a href="#" onclick="showSkillsPage()" >Skills</a></li> <li><a href="#" onclick="showProjectsPage()" >Projects</a></li> <li><a href="#" onclick="showLanguagesPage()" >Languages</a></li> </ul> <p id="home">I'm the home page.</p> <p id="skills">I'm the skills page.</p> <p id="projects">I'm the projects page.</p> <p id="languages">I'm the languages page.</p>


As one last step, you can move this logic into a single event handler.作为最后一步,您可以将此逻辑移动到单个事件处理程序中。 Instead of listening for clicks on the items, listen for clicks on the NavBar itself and use the Event.target property of the event to determine which item was clicked.不是监听项目的点击,而是监听 NavBar 本身的点击,并使用事件的Event.target属性来确定点击了哪个项目。

You'll need to use something like custom data attributes to keep track of the id to show/hide when an item is clicked.您需要使用自定义数据属性之类的东西来跟踪要在单击项目时显示/隐藏的id

 const targets = { "home": document.getElementById("home"), "skills": document.getElementById("skills"), "projects": document.getElementById("projects"), "languages": document.getElementById("languages") }; function showPage(event) { if (!event.target.dataset.targetId) return; Object.values(targets).forEach(item => item.style.display = "none"); targets[event.target.dataset.targetId].style.display = "block"; }
 #skills, #projects, #languages { display: none; }
 <ul id="top-menu-bar" onclick="showPage(event)"> <li><a href="#" data-target-id="home">Home</a></li> <li><a href="#" data-target-id="skills">Skills</a></li> <li><a href="#" data-target-id="projects">Projects</a></li> <li><a href="#" data-target-id="languages">Languages</a></li> </ul> <p id="home">I'm the home page.</p> <p id="skills">I'm the skills page.</p> <p id="projects">I'm the projects page.</p> <p id="languages">I'm the languages page.</p>

I completely overwrote your code to give you the ability to easily expand it.我完全覆盖了您的代码,让您能够轻松扩展它。

First, I removed your inline onclick events.首先,我删除了您的内联 onclick 事件。 Set the page ID as the HREF of the links.将页面 ID 设置为链接的 HREF。 Then I gave each page a class of page, that by default in CSS is hidden.然后我给每个页面一个页面类别,默认情况下在 CSS 中是隐藏的。 Then a new class for active that displays the page.然后是一个显示页面的活动新类。

the javascript adds an event listener to the parent nav then looks for an href on what was clicked. javascript 向父导航添加一个事件侦听器,然后在单击的内容上查找一个 href。 Then it goes through ALL visible pages (those that have .active) and removes active.然后它会遍历所有可见页面(具有 .active 的页面)并删除活动页面。 Then it adds the active class to the target page.然后它将活动类添加到目标页面。

This answer allows you to add, change the nav and pages as you need without needing to update your javascript.此答案允许您根据需要添加、更改导航和页面,而无需更新您的 javascript。

 let menuBar = document.querySelector("#top-menu-bar"); menuBar.addEventListener("click", function(e) { e.preventDefault(); let nav = e.target; let targetPage = nav.getAttribute("href"); if (targetPage) { let visible = document.querySelector(".page.active"); if (visible) { visible.classList.remove("active"); } let target = document.querySelector(targetPage); target.classList.toggle("active"); } });
 .page { display: none; } .page.active { display: block; }
 <ul id="top-menu-bar"> <li><a href="#home">Home</a></li> <li><a href="#skills">Skills</a></li> <li><a href="#projects">Projects</a></li> <li><a href="#languages">Languages</a></li> </ul> <p class="page active" id="home">I'm the home page.</p> <p class="page" id="skills">I'm the skills page.</p> <p class="page" id="projects">I'm the projects page.</p> <p class="page" id="languages">I'm the languages page.</p>

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

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