看看这个:-
现在我将偏好存储在localstorage
.
有关 DOM 存储选项,请参阅 Ref。但是在您的情况下,cookie 可能是一个安全的选择,或者将其存储在服务器中。
演示
存储逻辑
if (localStorage.activeTab) {//see if a tab has been stored in the localStorage
$('.active').removeClass('active');
$(".tabs li:nth-child(" + (parseInt(localStorage.activeTab, 10) + 1) + ") a.tab").addClass('active'); //Select that tab based on preference.
}
localStorage.activeTab = $(this).parent().index(); //Store the tab in the storage.
完整脚本
$(document).ready(function () {
if (localStorage.activeTab) {//see if a tab has been stored in the localStorage
$('.active').removeClass('active');
$(".tabs li:nth-child(" + (parseInt(localStorage.activeTab, 10) + 1) + ") a.tab").addClass('active'); //Select that tab based on preference.
}
// When a link is clicked
$("a.tab").click(function () {
// switch all tabs off
$(".active").removeClass("active");
// switch this tab on
$(this).addClass("active");
localStorage.activeTab = $(this).parent().index(); //Store the tab in the storage.
// slide all content up
$(".content").slideUp();
// slide this content up
var content_show = $(this).attr("title");
$("#" + content_show).slideDown();
});
});