欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

cookie 保存和清除用户样式

程序员文章站 2022-03-13 13:53:58
...

cookie 保存和清除用户样式

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. .container {
  8. border: 1px solid #ccc;
  9. width: 300px;
  10. margin: 20px auto;
  11. text-align: center;
  12. }
  13. .container > h1 {
  14. margin: 100px auto;
  15. }
  16. body > form {
  17. width: 350px;
  18. margin: 20px auto;
  19. display: flex;
  20. justify-content: space-between;
  21. align-items: center;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <form action="">
  27. <div>
  28. <div>
  29. <label for="bg">背景 rgb</label>
  30. <input type="text" name="bg1" id="bg1" size="3" value="255">
  31. <input type="text" name="bg2" id="bg2" size="3" value="255">
  32. <input type="text" name="bg3" id="bg3" size="3" value="255">
  33. </div>
  34. <div>
  35. <label for="color">字体 rgb</label>
  36. <input type="text" name="color1" id="color1" size="3" value="0">
  37. <input type="text" name="color2" id="color2" size="3" value="0">
  38. <input type="text" name="color3" id="color3" size="3" value="0">
  39. </div>
  40. </div>
  41. <div>
  42. <input type="button" value="确定" id="ensure">
  43. <input type="button" value="清理" id="clear">
  44. </div>
  45. </form>
  46. <div class="container">
  47. <h1 class="text">Hello world!</h1>
  48. </div>
  49. <script>
  50. window.onload = initialize;
  51. function initialize() {
  52. var form = document.forms[0],
  53. bg1 = getCookie('bg1') || 255,
  54. bg2 = getCookie('bg2') || 255,
  55. bg3 = getCookie('bg3') || 255,
  56. color1 = getCookie('color1') || 0,
  57. color2 = getCookie('color2') || 0,
  58. color3 = getCookie('color3') || 0;
  59. // 表单赋值
  60. form.bg1.value = bg1;
  61. form.bg2.value = bg2;
  62. form.bg3.value = bg3;
  63. form.color1.value = color1;
  64. form.color2.value = color2;
  65. form.color3.value = color3;
  66. // 修改背景和字体颜色
  67. document.getElementsByClassName('container')[0].style.backgroundColor = `rgb(${bg1}, ${bg2}, ${bg3})`;
  68. document.getElementsByClassName('text')[0].style.color = `rgb(${color1}, ${color2}, ${color3})`;
  69. // 绑定点击改变和删除背景和字体颜色事件
  70. document.getElementById('ensure').onclick = changeAttr;
  71. document.getElementById('clear').onclick = removeAttr;
  72. }
  73. function changeAttr() {
  74. var form = document.forms[0],
  75. bg1 = form.bg1.value,
  76. bg2 = form.bg2.value,
  77. bg3 = form.bg3.value,
  78. color1 = form.color1.value,
  79. color2 = form.color2.value,
  80. color3 = form.color3.value;
  81. // cookie 保存背景
  82. setCookie('bg1', bg1);
  83. setCookie('bg2', bg2);
  84. setCookie('bg3', bg3);
  85. // cookie 保存字体颜色
  86. setCookie('color1', color1);
  87. setCookie('color2', color2);
  88. setCookie('color3', color3);
  89. // 修改背景和字体颜色
  90. document.getElementsByClassName('container')[0].style.backgroundColor = `rgb(${bg1}, ${bg2}, ${bg3})`;
  91. document.getElementsByClassName('text')[0].style.color = `rgb(${color1}, ${color2}, ${color3})`;
  92. }
  93. function removeAttr() {
  94. // cookie 删除背景
  95. delCookie('bg1');
  96. delCookie('bg2');
  97. delCookie('bg3');
  98. // cookie 删除字体颜色
  99. delCookie('color1');
  100. delCookie('color2');
  101. delCookie('color3');
  102. // 页面重载
  103. location.reload();
  104. }
  105. function getCookie(name) {
  106. var cookies = document.cookie ? document.cookie.split(';') : [];
  107. for (var i = 0; i < cookies.length; i++) {
  108. if (0 === cookies[i].trim().indexOf(name)) {
  109. return cookies[i].trim().split('=')[1].trimLeft();
  110. }
  111. }
  112. return '';
  113. }
  114. function setCookie(name, value, expiredays = 30) {
  115. var cookies = document.cookie ? document.cookie.split(';') : [], date = new Date();
  116. date.setTime(date.getTime() + expiredays * 24 * 60 * 60 * 1000);
  117. // 修改存在的 name, expires 值
  118. for (var i = 0; i < cookies.length; i++) {
  119. if (0 === cookies[i].trim().indexOf(name)) {
  120. var existName = true;
  121. cookies[i] = name + '=' + value;
  122. continue;
  123. }
  124. if (0 === cookies[i].trim().indexOf('expires')) {
  125. var existExpires = true;
  126. cookies[i] = 'expires=' + date.toUTCString();
  127. continue;
  128. }
  129. if (0 === cookies[i].trim().indexOf('path')) {
  130. var existPath = true;
  131. }
  132. }
  133. // 添加未存在的 name, expires, path 值
  134. if ('undefined' === typeof existName) {
  135. cookies.unshift(name + '=' + value);
  136. }
  137. if ('undefined' === typeof existExpires) {
  138. cookies.push('expires=' + date.toUTCString());
  139. }
  140. if ('undefined' === typeof existPath) {
  141. cookies.push('path=/');
  142. }
  143. // 设置 cookie
  144. document.cookie = cookies.join(';');
  145. }
  146. function delCookie(name) {
  147. var cookies = document.cookie ? document.cookie.split(';') : [], date = new Date();
  148. date.setTime(date.getTime() - 1);
  149. // 删除存在的 name 值,修改 expires 值
  150. for (var i = 0; i < cookies.length; i++) {
  151. if (0 === cookies[i].trim().indexOf(name)) {
  152. cookies.splice(i, 1);
  153. }
  154. if (0 === cookies[i].trim().indexOf('expires')) {
  155. var existExpires = true;
  156. cookies[i] = 'expires=' + date.toUTCString();
  157. }
  158. if (0 === cookies[i].trim().indexOf('path')) {
  159. var existPath = true;
  160. }
  161. }
  162. // 如果未存在 expires, path 值
  163. if ('undefined' === typeof existExpires) {
  164. cookies.push('expires=' + date.toUTCString());
  165. }
  166. if ('undefined' === typeof existPath) {
  167. cookies.push('path=/');
  168. }
  169. // 设置 cookie
  170. document.cookie = cookies.join(';');
  171. }
  172. </script>
  173. </body>
  174. </html>

cookie 保存和清除用户样式

  • 改变背景黄色和字体红色rgb值

cookie 保存和清除用户样式

  • 刷新页面仍保持黄色背景,红色字体,点击清理,恢复原来样式