試試這個
$(document).ready(function() {
var key = 'clickedHref';
$("#clearButton").click(function() {
localStorage.setItem(key, null);
});
$('#accordian li').click(function() {
$("#accordian li").removeClass("active1");
var href = $(this).addClass("active1").children("a").attr("href");
localStorage.setItem(key, href);
});
var clickedHref = localStorage.getItem(key);
if (clickedHref !== null) {
var a = $('#accordian li a[href="' + clickedHref + '"]');
if (a.length) {
a.trigger("click");
}
}
});
CSS:
.active1 a{
font-weight:bold;
}
HTML:
<ul id="accordian">
<li>
<a href="?a=1">a1</a>
</li>
<li>
<a href="?a=2">a2</a>
</li>
</ul>
<button id='clearButton'>clear</button>
http://codepen.io/anon/pen/vGVjjP
您可以只更改頁面的某些部分或使用localStorage。 – Ciprianis
我需要在asp.net mvc母版頁中實現此功能。 – user300485