使用本地存储进行活动类
问题说明
如何将所选菜单项的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.
正确答案
好像你想要记住哪一个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
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20