Blogger Information
Blog 10
fans 0
comment 0
visits 4915
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
localstroage的保存用户设置
人生如梦
Original
439 people have browsed it

修改背景颜色和字号 并存入localstroage, 打开页面首先读取localstroage保存的数据,如果没有就读默认值

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. .word {
  8. width: 300px;
  9. height: 300px;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <div class="wrap">
  15. <div class="word">hello world</div>
  16. <button id="bg">修改背景颜色</button>
  17. <button id="size">修改字号</button>
  18. </div>
  19. <script>
  20. var wordObj = document.querySelector('.word')
  21. var bgColor = localStorage.getItem('bgColor') || '#fe3212';
  22. var fontSize = localStorage.getItem('fontSize') || '20px';
  23. wordObj.style.background = bgColor;
  24. wordObj.style.fontSize = fontSize;
  25. document.querySelector('#bg').onclick = function () {
  26. bgColor = prompt('请输入要修改的颜色');
  27. localStorage.setItem('bgColor', bgColor);
  28. wordObj.style.background = bgColor;
  29. }
  30. document.querySelector('#size').onclick = function () {
  31. fontSize = parseInt(prompt('请输入要修改的字体大小')) + 'px';
  32. localStorage.setItem('fontSize', fontSize);
  33. wordObj.style.fontSize = fontSize;
  34. }
  35. </script>
  36. </body>
  37. </html>

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post