简体   繁体   English

列表项在单击链接或重新加载/刷新页面时保持打开状态

[英]list item stay open on click a link or reload/refresh page

I want that my unordered list stays open if I click a list-item/link or I refresh/reload the page/site.如果我单击列表项/链接或刷新/重新加载页面/站点,我希望我的无序列表保持打开状态。 And the list-item/link should have the class active.列表项/链接应该激活 class。

Saw something, that I can store data in local-storage , but I do not know how.看到一些东西,我可以将数据存储在local-storage中,但我不知道如何。

HTML: HTML:

<aside id="sidebar" class="col-12 col-lg-4 col-xxl-3">
  <div class="just-padding">

    <ul>
      <li  id="server">Server</li>
        <ul>
          <li id="multiCollapseExample1">
            <span>
              <a href="{{ route('ubuntustart') }}">Start</a>
            </span>
          </li>
          <li id="multiCollapseExample2">
            <span>
              <a href="{{ route('ubuntuinit') }}">Security</a>
            </span>
          </li>
        </ul>
    
      <li id="git">GIT</li>
        <ul>
          <li id="multiCollapseExample3">
            <span>
              <a href="#1">blub</a>
            </span>
          </li>
          <li id="multiCollapseExample4">
            <span>
              <a href="#2">wurst</a>
            </span>
          </li>
        </ul>
    </ul>
  </div>
</aside>

jQuery: jQuery:

$(document).ready(function () {
   
    $("#multiCollapseExample1").hide();
    $("#multiCollapseExample2").hide();
    $("#multiCollapseExample3").hide();
    $("#multiCollapseExample4").hide();


    $("#server").click(function() {
        
        localStorage.setItem('clickedItem', '#server');
        
        $("#multiCollapseExample3, #multiCollapseExample4").hide();
        $("#multiCollapseExample1, #multiCollapseExample2").show();
        
    });

    $("#git").click(function() {
        
        localStorage.setItem('clickedItem', '#git');
        
        $("#multiCollapseExample1, #multiCollapseExample2").hide();
        $("#multiCollapseExample3, #multiCollapseExample4").show();
        
    });

    $("#multiCollapseExample1").click(function() {

        localStorage.setItem('clickedSub', '#multiCollapseExample1');

        $("#multiCollapseExample1, #multiCollapseExample2").show();
        
    });

    $("#multiCollapseExample2").click(function() {

        localStorage.setItem('clickedSub', '#multiCollapseExample2');

        $("#multiCollapseExample1, #multiCollapseExample2").show();
        
    });



    if(localStorage.getItem('clickedSub')) {

        var test = $(localStorage.getItem('clickedSub')).click()
        
        $(test).addClass("active");
    
    };

    
    
    if(localStorage.getItem('clickedItem')) {

        $(localStorage.getItem('clickedItem')).click()
        
        $(this).addClass("active");
    
    };


});

I have edited my answer (the jquery part)我已经编辑了我的答案(jquery 部分)

A new JSFiddle: JSFiddle一个新的 JSFiddle: JSFiddle

The active list item in red do not save the state if I go back (backwards / browser arrow).如果我返回 go(向后/浏览器箭头),则红色的活动列表项不会保存 state。 But it keep the state on reload/refresh.但它使 state 保持重新加载/刷新。

I have modified your code as you wanted.我已经根据需要修改了您的代码。 Please check jsfiddle请检查jsfiddle

What i did was just set the clicked item to localstorage.我所做的只是将点击的项目设置为本地存储。 I have saved its id.我已经保存了它的ID。 本地存储 . .

Then when refresh the page i check does localstorage exist the key added to localstorage(it should exist if you clicked a item).然后,当刷新页面时,我检查 localstorage 是否存在添加到 localstorage 的密钥(如果您单击某个项目,它应该存在)。 Then based on that value I have clicked the related item via click() method.然后根据该值,我通过click()方法单击了相关项目。

Just modified your Jquery as bellow刚刚修改了你的 Jquery 如下

$(document).ready(function () {

$("#multiCollapseExample1").hide();
$("#multiCollapseExample2").hide();
$("#multiCollapseExample3").hide();
$("#multiCollapseExample4").hide();


$("#server").click(function() {
    localStorage.setItem('clickedItem', '#server');
    $("#multiCollapseExample3, #multiCollapseExample4").hide();
    $("#multiCollapseExample1, #multiCollapseExample2").show();
    
});

$("#multiCollapseExample1").click(function() {
    $("#multiCollapseExample1, #multiCollapseExample2").show();
    $("#multiCollapseExample1").addClass('active');
    
});

$("#git").click(function() {
    localStorage.setItem('clickedItem', '#git');
    $("#multiCollapseExample1, #multiCollapseExample2").hide();
    $("#multiCollapseExample3, #multiCollapseExample4").show();
    
});


$("li span").click(function() {
      
    $(this).addClass("active");

});


  if(localStorage.getItem('clickedItem')) {
    $(localStorage.getItem('clickedItem')).click()
   }

}); });

By the way I dont recommend you to use ids to click event.顺便说一下,我不建议你使用 ids 来点击事件。 So if there are 100 items then based on your code you need to write more and more logic.因此,如果有 100 个项目,那么根据您的代码,您需要编写越来越多的逻辑。 Instead of that use another mechanism to achieve this .而不是使用另一种机制来实现这一点 Thanks谢谢

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

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