如果BG-颜色有改变,你可以使用localStorage
检查BG为被重新加载之前,页面的内容:
var colours = ['#F00','#0F0'];//my eyes!
var currentColour = +(localStorage.previousBGColour || -1)+1;
currentColour = currentColour >= colours.length ? 0 : currentColour;//if index is not set reset to colour at index 0
document.getElementById('theDiv').style.backgroundColor = colours[currentColour];
localStorage.previousBGColour = currentColour;//store colour that's currently in use
注意,并不是所有的浏览器都支持localStorage
:有些人还在使用旧,照出IE8,例如。
的jQuery
$(document).ready(function()
{
(function()
{//this IIFE is optional, but is just a lot tidier (no vars cluttering the rest of the script)
var colours = ['#F00','#0F0'],
currentColour = +(localStorage.previousBGColour || -1) + 1;
$('#theDiv').css({backgroundColor:colours[currentColour]});
localStorage.previousBGColour = currentColour;
}());
}
像你一样,请不要使用数组构造函数:,而不是写'新的Array()',使用的文字符号:'[]'。短得多,更可预测。对象也一样:使用'{}'而不是'new Object' –