简体   繁体   English

保持活跃的药丸刷新,Bootstrap 4

[英]Keep active pill on refresh, Bootstrap 4

I am creating a website using Bootstrap 4, and for my navigation bar, I am using pills for my buttons.我正在使用 Bootstrap 4 创建一个网站,对于我的导航栏,我正在为我的按钮使用药丸。 The problem I want to fix is that if I am on a different tab and I refresh the page, it jumps back to the home page.我要解决的问题是,如果我在不同的选项卡上并刷新页面,它会跳回主页。 I have seen many solutions for this for people who have used tabs instead, but not for pills.对于那些使用标签而不是药片的人,我已经看到了许多解决方案。 Here is the snippet of my code that other people usually provide for this kind of problem:这是其他人通常为此类问题提供的我的代码片段:

<div class="collapse navbar-collapse" id="navbarResponsive">
  <ul class="nav nav-pills navbar-nav ml-auto" id="pills-tab" role="tablist">
    <li class="nav-item">
      <a class="nav-link active" id="pills-home-tab" data-toggle="pill" href="#pills-home" role="tab" aria-controls="pills-home" aria-selected="true">Home</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" id="pills-education-tab" data-toggle="pill" href="#pills-education" role="tab" aria-controls="pills-education" aria-selected="false">Education</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" id="pills-skills-tab" data-toggle="pill" href="#pills-skills" role="tab" aria-controls="pills-skills" aria-selected="false">Skills</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" id="pills-activities-tab" data-toggle="pill" href="#pills-activities" role="tab" aria-controls="pills-activities" aria-selected="false">Activities</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" id="pills-projects-tab" data-toggle="pill" href="#pills-projects" role="tab" aria-controls="pills-projects" aria-selected="false">Projects</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" id="pills-contact-tab" data-toggle="pill" href="#pills-contact" role="tab" aria-controls="pills-contact" aria-selected="false">Contact</a>
    </li>
  </ul>
</div>

It would be much appreciated if one of you guys can help out with this problem... Thanks in advance!如果你们中的一个人能帮助解决这个问题,将不胜感激......提前致谢!

<a class="nav-link active" id="pills-home-tab" data-toggle="pill" href="#pills-home" role="tab" aria-controls="pills-home" aria-selected="true">Home</a>

Your html specifies the initial active tab by putting the "active" class in the first pill.您的 html 通过将“活动” class 放在第一个药丸中来指定初始活动选项卡。 As that is clicked, bootstrap handles taking that class off and adding it to the new pill.单击该按钮后,引导程序会处理关闭 class 并将其添加到新药丸中。

You need to set an onClick function for when one of the links to switch pills is clicked.当单击切换药丸的链接之一时,您需要设置 onClick function。 That function should set a cookie or use local storage to record which pill is the latest to get the "active" class. function 应该设置一个 cookie 或使用本地存储来记录哪个药丸是最新获得“活动”class。

<a class="nav-link" id="pills-home-tab" data-toggle="pill" href="#pills-home" role="tab" aria-controls="pills-home" aria-selected="true" onClick="return saveTabSelect(this);">Home</a>

This tells the link click to call the function to store the setting这告诉链接单击调用 function 来存储设置

function saveTabSelect(e) {
  localStorage.setItem("tagSelected", e.id);
  return true;
}

You need to have function that runs on page Load that looks for the cookie or in local storage and then assigns the active class to the tab/pill specified or default to whatever if no setting is found.您需要让 function 在页面 Load 上运行以查找 cookie 或在本地存储中,然后将活动的 class 分配给指定的选项卡/药丸,如果没有找到任何设置,则默认为任何设置。 And because the script will assign that class, you want to pull it out of the html itself.并且由于脚本将分配 class,因此您希望将其从 html 本身中拉出。

function retrieveSelected(){
  var curTag = localStorage.getItem("tagSelected");
  var element = document.getElementById(curTag);
  element.classList.add("active"); 
}

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

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