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

localStorage使用

程序员文章站 2022-03-03 22:37:38
...

1.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <div id="div" style="height: 200px;width: 200px" >
  9. <button id="button">点击</button>
  10. <button id="button2">点击</button>
  11. </div>
  12. <script>
  13. var div= document.getElementById("div");
  14. var button= document.getElementById("button");
  15. var button2= document.getElementById("button2");
  16. var bgcolor = window.localStorage.getItem('bgcolor');
  17. console.log(bgcolor)
  18. button.onclick=function () {
  19. bgcolor =prompt("请输入颜色");
  20. window.localStorage.setItem('bgcolor',bgcolor);
  21. div.style.background = bgcolor;
  22. }
  23. button2.onclick=function () {
  24. parent.location="2.html"
  25. }
  26. </script>
  27. </body>
  28. </html>

2.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>2</title>
  6. </head>
  7. <body id="body">
  8. <script>
  9. onload=function () {
  10. bgcolor = window.localStorage.getItem('bgcolor');
  11. document.getElementById("body").style.background=bgcolor;
  12. }
  13. </script>
  14. </body>
  15. </html>