forked from lijinke666/javascript-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookie.html
54 lines (54 loc) · 1.41 KB
/
cookie.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>cookie</title>
<style>
body{
font-family: "Microsoft YaHei", Arial;
}
div{
float:left;
width: 200px;
height: 50px;
text-align: center;
line-height: 50px;
color:#FFF;
}
.red{
background: #ff696e;
margin-right:10px;
}
.green{
background: #1f8060;
}
</style>
</head>
<body>
<div class="red bg">红</div>
<div class="green bg">绿</div>
</body>
<script>
window.onload=function(){
var background = document.cookie.split("=").pop();
if( background==""){
document.body.style.backgroundColor="#FFF";
}else{
document.body.style.backgroundColor=background;
}
var bg = document.getElementsByClassName("bg");
for(var i=0; i<bg.length; i++){
bg[i].onclick=function(){
var color = this.getAttribute("class");
if(color.indexOf("red")>-1){
document.body.style.backgroundColor="#ff696e";
document.cookie="bgColor=#ff696e";
}else{
document.body.style.backgroundColor="#1f8060";
document.cookie="bgColor=#1f8060";
}
}
}
}
</script>
</html>