Blogger Information
Blog 119
fans 3
comment 1
visits 95162
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
localStorage实现本地存储读取CSS样式
赵大叔
Original
696 people have browsed it

localStorage 本地储存

  • 相当于一个本地数据库

第一个页面代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>用户设置自定义CSS样式</title>
  8. </head>
  9. <body>
  10. <div style="margin: auto;width: 400px;">
  11. <input type="button" onclick="setBgColor()" value="设置首页背景色">
  12. <input type="button" onclick="setHeight()" value="设置高度"><br><br>
  13. <a href="demo2.html">第二个页面</a>
  14. </div>
  15. </body>
  16. </html>
  17. <script>
  18. // localStorage 本地储存网页背景、字体颜色
  19. function setBgColor() {
  20. var color = window.prompt("请输入背景颜色");
  21. localStorage.setItem('bgcolor', color);
  22. }
  23. function setHeight() {
  24. height = window.prompt("请输入高度");
  25. localStorage.setItem('height', height);
  26. }
  27. </script>

第二个页面代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>第二个页面</title>
  8. <style id="style"></style>
  9. </head>
  10. <body>
  11. <div id="content" style="margin: auto;width: 400px;">
  12. </div>
  13. </body>
  14. </html>
  15. <script>
  16. window.onload=function(){
  17. var bgcolor= localStorage.getItem('bgcolor');
  18. var height = localStorage.getItem('height');
  19. var style = document.getElementById('style');
  20. style.innerText=' #content {background-color:' + bgcolor + '; height:' + height + ';}';
  21. }
  22. </script>

从第一个页面点击进入第二个页面的效果

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