• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

使用本地存储进行活动类

用户头像
it1352
帮助1

问题说明

如何将所选菜单项的Cookie存储在本地存储中?

How do I keep cookie stored of selected menu item with local storage?

菜单 -

<ul class="nav nav-pills">
    <li class="active"><a href="https://www.it1352.com/1022805.html" >Customers</a></li>
    <li><a href="https://www.it1352.com/1022805.html" >Statics</a></li>
    <li><a href="https://www.it1352.com/1022805.html" >payroll</a></li>
</ul>

切换活动类 -

<script type="text/javascript">
    $(function () {
        $('.nav-pills li').click(function () {
            $(this).siblings().removeClass('active');
            $(this).addClass('active');
        });
    });
</script>

使用localstorage我试过 -

Using localstorage I tried-

<script type="text/javascript">
    $(function () {
        $('.nav-pills li').click(function () {
            $(this).siblings().removeClass('active');
                var menuactive = localStorage.setItem('mySelectValue', $(this).addClass('active');
                $(this).addClass(localStorage.getItem('mySelectValue'));
            });
        });
</script>

现在这对我没有帮助。

如何在本地存储中存储活动类?当刷新页面时,我会丢失此活动类。
这个问题被问过多次,但我没有得到任何简单的方法。

How do I store active class in local storage? I lose this active class when page is refreshed. This question is asked many times but I didn't get any simple way of doing it.

正确答案

#1

好像你想要记住哪一个element有(上次,刷新前)'active'类。

It seems like you want to remember which element had (last time, before refresh) the 'active' class.

所以如果你有一个已知数量的列表项,你可以跟踪索引和存储它。

So if you've got a known number of list items, you could just keep track of the index, and store that.

$(function () {
    $('.nav-pills li').click(function () {

        $(this).siblings().removeClass('active');
        $(this).addClass('active');

        var activeIndex = $(this).index();
        localStorage.setItem('mySelectValue', activeIndex);
    });

然后你可以有一个onload函数,就像这样...

And then you can have an onload function, something like this...

    var activeIndex = localStorage.getItem('mySelectValue');
    if (isNaN(activeIndex)) {
        console.log('nothing stored');
    } else {
        $('.nav-pills li:nth-child(' activeIndex ')').addClass('active');
    }

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfkjkg
系列文章
更多 icon
同类精品
更多 icon
继续加载