0
我有一个日历,日历中可点击。每次点击它都会改变颜色(白色默认,红色和绿色 - 环形)。如何将属性保存在html中(无Cookie - 只是本地存储)
<script type="text/javascript">
function inArray(needle, haystack) {
if (!needle || !haystack) {
return false;
}
else {
var notFound = -1;
for (var i = 0, len = haystack.length; i < len; i++) {
if (haystack[i] == needle) {
return i;
}
}
return notFound;
}
}
function colorToggle(el, colors) {
if (!el) {
return false;
}
else {
var colors = colors || [
'rgb(255, 0, 0)',
'rgb(0, 255, 0)',
'rgb(0, 0, 255)'],
wGCS = window.getComputedStyle,
curColor = wGCS(el, null).backgroundColor;
var pos = inArray(curColor, colors);
if (pos > -1 && pos < (colors.length - 1)) {
el.style.backgroundColor = colors[inArray(curColor, colors) + 1];
}
else if (pos > -1 && pos == (colors.length - 1)) {
el.style.backgroundColor = colors[0];
}
}
}
var buttons = document.querySelectorAll('button.colorToggle');
for (var i = 0, len = buttons.length; i < len; i++) {
buttons[i].style.backgroundColor = 'rgb(255, 255, 255)';
buttons[i].onclick = function() {
colorToggle(this, ['rgb(255, 255, 255)','rgb(0, 255, 0)','rgb(255, 0, 0)']);
};
}
</script>
现在,我想保持的颜色后刷新/重新打开index.html页面。
谢谢你的帮助。