<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body onload="colorload();"> <script> if(typeof localStorage === 'undefiend'){ window.alert("you brower 不支持 localStorage"); }else{ window.alert("you brower 支持 localStorage"); var storage = localStorage; function redSet(){ var value = 'red'; document.getElementById("divblock").style.backgroundColor = value; window.localStorage.setItem('DivBackGroundColor',value); } function greenSet(){ var value="green"; document.getElementById("divblock").style.backgroundColor = value; window.localStorage.setItem("DivBackGroundColor",value); } function blueSet(){ var value = "blue"; document.getElementById("divblock").style.backgroundColor = "blue"; window.localStorage.setItem("DivBackGroundColor",value); } function colorload(){ document.getElementById( "divblock").style.backgroundColor = window.localStorage.getItem("DivBackGroundColor"); } } </script> <button id="redbtton" onclick="redSet()">红色</button> <button id="redbtton" onclick="greenSet()">绿色</button> <button id="redbtton" onclick="blueSet()">蓝色</button> <div id="divblock" style="500px;height:500px;"></div> </body> </html>