[英]How to keep active class when changing pages
I am trying to add an active class to nav item, depending what page your on.我正在尝试向导航项目添加一个活动类,具体取决于您所在的页面。 I am using this script:
我正在使用这个脚本:
<script type="text/javascript">
$(document).ready(function () {
$("#side-bar a").click(function () {
var id = $(this);
$(id).siblings().find(".active").removeClass("active");
$(id).addClass("active");
localStorage.setItem("selectedolditem", id);
});
var selectedolditem = localStorage.getItem('selectedolditem');
if (selectedolditem !== null) {
$(selectedolditem).siblings().find(".active").removeClass("active");
$(selectedolditem).addClass("active");
}
});
</script>
See full jsfiddle here: https://jsfiddle.net/ebo7hLo9/ It adds the active class, but it loads a new page, it disappears.在这里查看完整的 jsfiddle: https ://jsfiddle.net/ebo7hLo9/ 它添加了活动类,但它加载了一个新页面,它消失了。 What am I doing wrong?
我究竟做错了什么?
https://jsfiddle.net/ebo7hLo9/10/ - here's a fiddle! https://jsfiddle.net/ebo7hLo9/10/ - 这是一个小提琴!
$(document).ready(function () {
$("#side-bar a").click(function () {
var id = $(this);
$(".active").removeClass("active");
$(id).addClass("active");
localStorage.setItem("selectedolditem", $(id).text());
});
var selectedolditem = localStorage.getItem('selectedolditem');
if (selectedolditem !== null) {
$("a:contains('" + selectedolditem + "')").addClass("active");
}
});
Your code was having issues remembering what element to grab.您的代码在记住要抓取的元素时遇到问题。 I think it's due to the web storage API's unfamiliarity with objects.
我认为这是由于 Web 存储 API 不熟悉对象。 Instead I got the text from the element that was selected, stored it in localStorage and on page load matched it with the correct element.
相反,我从所选元素中获取文本,将其存储在 localStorage 中,并在页面加载时将其与正确的元素匹配。 Also there was part of your code that was dealing with finding the class "active" within the
siblings()
of the selected element and removing it.此外,您的部分代码正在处理在所选元素的
siblings()
中查找“活动”类并将其删除。 That complex of code is largely unnecessary.这种复杂的代码在很大程度上是不必要的。 I replaced it with the class selector
$(".active")
我用类选择器
$(".active")
替换了它
I didn't change this in the code, but I'd advise against using localStorage
in favor of sessionStorage
so that the storage will clear itself on tab/browser close.我没有在代码中更改这一点,但我建议不要使用
localStorage
来支持sessionStorage
这样存储将在选项卡/浏览器关闭时自行清除。
For more info take a look at this previous stackoverflow post: Storing Objects in HTML5 localStorage有关更多信息,请查看之前的 stackoverflow 帖子: 在 HTML5 localStorage 中存储对象
Here is a possible solution: https://jsfiddle.net/6yyLpma1/这是一个可能的解决方案: https : //jsfiddle.net/6yyLpma1/
$("#side-bar a").click(function () {
var id = $(this);
$('#side-bar').find(".active").removeClass("active");
$(id).addClass("active");
localStorage.setItem("selectedolditem", id);
});
Instead of $(id).siblings()
use $('#side-bar')
.而不是
$(id).siblings()
使用$('#side-bar')
。 Use the same logic in other location.在其他位置使用相同的逻辑。
Using data elements and a delegate function: https://jsfiddle.net/ebo7hLo9/12/使用数据元素和委托函数: https : //jsfiddle.net/ebo7hLo9/12/
HTML HTML
<span id="delegateAnchor">
<div id="side-bar">
<ul>
<li><a href="#" data-desc="home">Home</a></li>
<li><a href="#" data-desc="whoAreWe">Who we are</a></li>
<li><a href="#" data-desc="services">Services</a></li>
<li><a href="#" data-desc="whatToExpect">What to expect</a></li>
<li><a href="#" data-desc="representClients">Representative clients</a></li>
<li><a href="#" data-desc="successStories">Success stories</a></li>
<li><a href="#" data-desc="currentLit">Current litigation</a></li>
<li><a href="#" data-desc="whatIfYouCould">What if you could not be a doctor?</a></li>
</ul>
</div>
</span>
Javascript Javascript
$(document).ready(function () {
$('#delegateAnchor').on('click', '#side-bar a', function() {
var $this = $(this);
var linkId = $this.data('desc');
$this.closest('ul').find('a').removeClass("active");
$this.addClass("active");
localStorage.setItem("menuSelection", linkId);
});
var selectedLinkId = localStorage.getItem("menuSelection");
if (selectedLinkId !== null) {
$('#side-bar a[data-desc="'+ selectedLinkId +'"]').trigger("click");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.