简体   繁体   English

如何在导航栏上获取当前元素并保存在localStorage上?

[英]How do I get the current element on nav bar and save on localStorage?

I have a nav bar, this has a links to pages, I want to save the current nav active when I clicked on that, this because I have a same menu in the other pages. 我有一个导航栏,它具有页面链接,我想在单击时保存当前的导航,这是因为其他页面具有相同的菜单。

<ul>
 <li id="nav1" class="navigation-item"><a href="#" >Nav 1</a></li>
 <li class="navigation-item"><a href="#" >Nav 2</a></li>
 <li class="navigation-item"><a href="#" >Nav 2</a></li>
 <li class="navigation-item"><a href="#" >Nav 2</a></li>
</ul>

I try to get from $(this) and then use after the page is reload in order to add the class active. 我尝试从$(this) ,然后在页面重新加载后使用以添加活动类。

$(document).ready(function () {

  $('.navigation-item').on('click', function (e) {
      var current = $( this );
      console.log(current);
      console.log(JSON.stringify(current));
      localStorage.setItem('activeTab', current);
  });

  var activeTab = localStorage.getItem('activeTab');

  if (activeTab) {
   // activeTab.addClass('active'); 
   // $('#nav1').parent().addClass('active'); 
      console.log(activeTab);
      $( "ul li:nth-child(2)" ).append( "<span> - 2nd!</span>" );
  }

});

jsFiddle 的jsfiddle

Best Regards. 最好的祝福。

You need to store the index of the element: 您需要存储元素的索引:

$('.navigation-item').on('click', function (e) {
    var current = $(this );
    console.log(current);
    console.log(JSON.stringify(current));
    localStorage.setItem('activeTab', $('.navigation-item').index(current));

});

And then, use the index to get back the element: 然后,使用索引取回元素:

var activeTab = localStorage.getItem('activeTab');
if (activeTab) {
        activeTabEL = $('.navigation-item').eq(parseInt(activeTab));
   activeTabEL.addClass('active'); 
  // $('#nav1').parent().addClass('active'); 
    console.log(activeTabEL);
    $( "ul li.navigation-item" ).eq(parseInt(activeTab)).append( "<span> - 2nd!</span>" );
}

Example: https://jsfiddle.net/xmzb7hdk/7/ 示例: https//jsfiddle.net/xmzb7hdk/7/

You don't need much JavaScript. 您不需要太多的JavaScript。 You can use an HTML5 custom attribute to identify each element in the nav ( data-nav-item ): 您可以使用HTML5自定义属性来标识导航( data-nav-item )中的每个元素:

<ul>
    <li data-nav-item="nav-1" id="nav1" class="navigation-item"><a href="#" >Nav 1</a></li>
    <li data-nav-item="nav-2" class="navigation-item"><a href="#" >Nav 2</a></li>
    <li data-nav-item="nav-3" class="navigation-item"><a href="#" >Nav 2</a></li>
    <li data-nav-item="nav-4" class="navigation-item"><a href="#" >Nav 2</a></li>
</ul>

Then it's just a simple 3 lines of JavaScript to set the value: 然后,只需三行JavaScript即可设置值:

$(document).on("click", "[data-nav-item]", function(event) {
    localStorage.setItem("activeTab", this.getAttribute("data-nav-item"));
});

After that, you can identify the active nav using: 之后,您可以使用以下命令识别活动导航

var activeTab = $("[data-nav-item='" + localStorage.getItem("activeTab") + "']")

You can store the id Instead of storing the whole object. 您可以存储id而不是存储整个对象。 It simplifies the use on retrieval. 它简化了检索的使用。

Fiddle here 在这里摆弄

HTML: HTML:

<ul>
  <li id="nav1" class="navigation-item"><a href="#" >Nav 1</a></li>
  <li id="nav2" class="navigation-item"><a href="#" >Nav 2</a></li>
  <li id="nav3" class="navigation-item"><a href="#" >Nav 3</a></li>
  <li id="nav4" class="navigation-item"><a href="#" >Nav 4</a></li>
</ul>

Script: 脚本:

$(document).ready(function () {

    $('.navigation-item').on('click', function (e) {
        var currentID = $(this).attr("id");
        console.log(currentID+" = stored to localStorage");
        localStorage.setItem('activeTab', currentID);

    });

    var activeTab = localStorage.getItem('activeTab');
    if (activeTab!="") {
        console.log(activeTab+" = retrieved form localStorage");
        $("#"+activeTab ).append( "<span> - active!</span>" );
        $("#"+activeTab ).addClass('active');
    }

});

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

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