【发布时间】:2021-12-26 04:10:03
【问题描述】:
我有一个按钮可以改变背景颜色和标题颜色
button id="change" onclick="change()">Change background!</button>
var state = true;
function change(){
if (state){
document.body.style.backgroundColor = "red";
document.getElementById('title').style.color='blue';
} else {
document.body.style.backgroundColor = "#005fff";
document.getElementById('title').style.color='lightgreen';
}
state = !state;
}
我使用状态在一个和第二个选项之间进行选择,如果您有更好的解决方案请告诉我。
我想将设置保存在本地存储中(刷新并退出后我想保留选择的背景)。
【问题讨论】:
标签: javascript local-storage web-storage