我读过关于使用localStorage的文章,但似乎无法让它工作,也从未真正理解它是如何工作的。 如果有人能帮我做这件事,我将不胜感激。
<link rel="stylesheet" href="darkmodestyle.css" id="darkmodestyle" disabled="disabled">
。
$(document).ready(function (){
$(".darkmode-toggle").click(function () {
if ($("#darkmodestyle").prop("disabled")) {
$('#darkmodestyle').removeAttr("disabled");
}
else {
$('#darkmodestyle').attr("disabled", "disabled");
}
});
});
因此,听起来您希望在多个会话之间持久化设置,因此我不建议使用SessionStorage。 Cookie旨在在请求期间发送到服务器。
您可以使用localstorage,它非常类似,但数据跨多个会话持久化。 使用LocalStorage.SetItem('Button-1','Dark Mode')
设置该值,使用LocalStorage['Button-1']
检索该值。 LocalStorage.RemoveItem('Button-1')
将删除该设置。