【问题标题】:Set button on next page to focus like the button clicked by the user将下一页上的按钮设置为像用户单击的按钮一样聚焦
【发布时间】:2017-09-26 10:34:35
【问题描述】:

我想知道是否有办法在用户打开的新页面上将焦点设置到上一页中单击的同一个按钮或 div。目前我有一个标题,其中包含在每个页面上都相同的顶部导航栏,但我想做的是当用户单击一个导航按钮并转到下一页时,该导航按钮保持突出显示(或焦点)在下一页。有没有办法做到这一点?

这是导航栏的当前代码:

div.scrollmenu {
  background-color: #333;
  overflow: auto;
  white-space: nowrap;
}

div.scrollmenu a {
  display: inline-block;
  color: white;
  text-align: center;
  padding: 14px;
  text-decoration: none;
}

div.scrollmenu a:hover {
  background-color: #777;
}

div.scrollmenu a:focus {
  background-color: #777;
}
<div class="scrollmenu">
  <a tabindex="1" href="#home">Home</a>
  <a tabindex="2" href="#news">News</a>
  <a tabindex="3" href="#contact">Contact</a>
  <a tabindex="4" href="#about">About</a>
  <a tabindex="5" href="#support">Support</a>
  <a tabindex="6" href="#blog">Blog</a>
  <a tabindex="7" href="#tools">Tools</a>
  <a tabindex="8" href="#base">Base</a>
  <a tabindex="9" href="#custom">Custom</a>
  <a tabindex="10" href="#more">More</a>
  <a tabindex="11" href="#logo">Logo</a>
  <a tabindex="12" href="#friends">Friends</a>
  <a tabindex="13" href="#partners">Partners</a>
  <a tabindex="14" href="#people">People</a>
  <a tabindex="15" href="#work">Work</a>
</div>

这样做的问题是当用户被重定向到下一页时页面会刷新标题导航,因此如果用户单击其中一个菜单选项卡,它会在当前页面上突出显示,但随后用户被导航到下一页及其不再突出显示或聚焦。基本上我想要它,所以当用户单击导航选项卡时,它会在当前页面和他们导航到的下一页上保持突出显示。

我可以使用 javascript、jquery,甚至只是普通的 html 和 css。

任何帮助都将不胜感激

【问题讨论】:

  • Element.focus().
  • 有三种方法,你必须在javascript中读取URL参数,或者在你的后端处理,或者使用LocalStorage为用户存储当前页面信息。

标签: javascript jquery html css


【解决方案1】:

最好的方法是创建一个activeclass,当你切换页面时你可以更新哪些链接应该是活动的,看看下面的例子

// Code goes here

$(function(){
  
  //watch for the changes in the url and update nav respectively
   $(window).hashchange(function(){
     var hash = location.hash.replace( /^#/, '' );
     alert(hash+"-"+location.hash);
     
     //update our nav now
     updateNav(location.hash);
     
   });
  
  function updateNav(hash){
    $("[data-active]").each(function (index, item){
    
    // get the active class name
      var activeClass = $(this).attr("data-active");
      //remove it from each 
      $(this).removeClass(activeClass);
       
      // get the href
      var href =$(this).attr("href");
      
      //compare and update the class if the hash is same with the href
     if(href == hash){
       $(this).addClass(activeClass); 
     }
    });
  }
  
  
  
});
.active {
  color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 
 
    <nav>
      <a data-active="active"  href="#/home">Home</a>
      <a data-active="active" href="#/about">About</a>
      <a data-active="active" href="#/page2">Page 2</a>
    </nav>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-hashchange/1.3/jquery.ba-hashchange.js"></script>
    <script src="script.js"></script>
  

【讨论】:

    【解决方案2】:

    最常见的方法是使用 Javascript/jQuery 或在您的服务器端代码中基于 URL 添加一个类 active,然后使用该类突出显示标题中的元素。

    【讨论】:

      猜你喜欢
      • 2016-06-12
      • 2020-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多