【问题标题】:Bootstrap sidebar navbar save opened tab after refreshBootstrap 侧边栏导航栏在刷新后保存打开的选项卡
【发布时间】:2017-03-22 21:07:34
【问题描述】:

我的引导网站中有一个使用引导 css 类的侧边菜单:

<div class="page-sidebar navbar-collapse collapse sidebar-nav">

<ul>
<li>
..
</li>
</ul>
</div>

如果用户点击了一个标签,页面刷新了,如何保存用户打开的标签?有什么想法吗?

【问题讨论】:

    标签: html angularjs twitter-bootstrap


    【解决方案1】:
    1. 如果你使用的是 Angular ui 路由器,你可以使用 stateparam,如果没有还有其他两个选项

    2. 网址参数,

    3. 本地存储,

    第二点

        function getParameterByName(name, url) {
        if (!url) {
          url = window.location.href;
        }
        name = name.replace(/[\[\]]/g, "\\$&");
        var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
            results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, " "));
    }
    
    
     url with param page ?tab=home
    $scope.tab = getParameterByName('tab'); // this is called in the controller if the page is reloaded
    

    ###编辑### 然后将参数添加到链接中

    <li ng-class="active: tab === 'home' href="someurl/?tab=home"></li>
    <li ng-class="active: tab === 'about' href="someurl/?tab=about"></li>
    

    第三个选项 您可以使用功能添加 ng-click

    $scope.setTab = function(tab){
        $window.localStorage.setItem("tab", tab);
    }
    

    你可以通过

    $window.localStorage.getItem("tab");
    

    如果选项卡值为空,则必须处理 null

    您可以使用sessionStorage 这是暂时的

     set value 
     sessionStorage.tab = "home"
      get value
      $scope.tab = sessionStorage.tab
    

    【讨论】:

    • 我对本地存储感兴趣,你能帮我解决一下吗?
    • 好的,我给你举个例子
    • @ElioChamy 如果您有任何不清楚的地方,请告诉我;)
    • 你好,还是有问题,如果我使用参数的第二个选项,我复制粘贴该功能,菜单将保存打开的选项卡,当我点击特定选项卡时应该调用它吗?
    • 是的,点击调用函数
    猜你喜欢
    • 2014-07-03
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 2020-08-09
    相关资源
    最近更新 更多