我想用HTML和JavaScript做一个基本的主题切换代码。但是,如果我回来或刷新,选择被取消,页面恢复正常。我可以将我的选择(主题开关)保存在HTML中吗?
我可以在JavaScript中添加任何函数,以便选择保持到缓存清理或类似的东西吗?
var x = 0;
var themeValue = 0;
function changeTheme3() {
themeValue ++;
if (themeValue > 2) {
themeValue = 1;
}
if (themeValue == 1) {
document.body.style.backgroundColor = "grey";
} else if (themeValue == 2) {
document.body.style.backgroundColor = "white";
}
}
button {
display: block;
margin: 0 auto;
padding: 0;
width: 250px;
height: 70px;
border: 2px solid #1ECD97;
border-radius: 40px;
background: transparent;
color: #1ECD97;
letter-spacing: 1px;
font-size: 18px;
font-family: 'Montserrat', sans-serif;
}
button:hover {
background-color: #1ECD97;
color: #fff;
}
<div id="theme-button">
<button type="button" onclick="changeTheme3()">Change Theme</button>
</div>
一想到twerking代码......我会在一分钟... – evolutionxbox
使用html5 localstorage或cookie,然后在加载时,您可以通过if语句检查保存的模板是否存在cookie /本地存储,如果是,则加载该模板,否则加载默认值 – StackOverMySoul
您需要使用cookie。 'document.cookie' – evolutionxbox