【问题标题】:Change Stylesheet and save it to "Cookies / Local storage"更改样式表并将其保存到“Cookies / 本地存储”
【发布时间】:2017-09-22 22:14:48
【问题描述】:

我有两个样式表

<link href="design/default.css" rel="stylesheet" type="text/css" title="default">
<link href="design/new.css" rel="stylesheet" type="text/css" title="new">

我有按钮来改变这些样式表:

<button class="" onclick="swapStyleSheet('design/default.css')">Default</button>
<button class="" onclick="swapStyleSheet('design/new.css')">New</button>

当然还有交换这些样式表的 javascript。

    function swapStyleSheet(sheet){
       document.getElementById('design').setAttribute('href', sheet);
}

问题是:

如何保存用户点击的样式表?使用 Cookies / 本地存储

即使用户离开我的页面并在 4 天后回来。

使用 "Cookies" 还是 "Local storage" 更好?

【问题讨论】:

    标签: javascript html cookies local-storage stylesheet


    【解决方案1】:

    windowload 事件中定义一个变量来存储对Blob URL 的引用。如果localStorage 有键,例如"styles",从localStorage 获取css,创建一个Blob,将localStorage.getItem("styles") 设置为第一个参数的可迭代元素,type 设置为"text/css"。将Blob 传递给URL.createObjectURL() 以设置#design .href 的值。否则,在swapStyleSheet 调用中过滤document.styleSheets 以匹配sheet 参数的.href,从.rules 属性查找中创建get .cssText,设置为Blob,执行上述过程。

    window.onload = function() {
      let blob, url;
    
      function setStyle() {
        var sheet = localStorage.getItem("styles");
        blob = new Blob([localStorage.getItem("styles")], {type:"text/css"});    
        url = URL.createObjectURL(blob);
        document.getElementById("design").setAttribute("href", url);
      }
    
      if (localStorage.getItem("style") != null) setStyle();
    
      function swapStyleSheet(sheet) {
    
        var rules = Array.prototype.filter.call(document.styleSheets, function(styles) {
          return styles.href.indexOf(sheet) !== -1
        })[0].rules;
    
        for (var i = 0, css = ""; i < rules.length; i++)  css += rules[i].cssText;
    
        if (url) URL.revokeObjectURL(url);
    
        localStorage.setItem("styles", css);
    
        setStyle();
    
      }
    }
    

    【讨论】:

    • 我还是不明白。 你当然解释得很好。如果我单击“新建”按钮更改样式并保留它,即使我重新加载页面,我只想保存样式表。我不懂 cookie 和本地存储。
    • 您阅读规范Web storage了吗?
    • 我现在就读。英语对我来说很难,但我会看看我能做什么
    • 不用担心。慢慢来。
    • 好吧,我没有得到太多信息。你能帮我写代码吗?请。作为一个 13 岁的小伙子,真的很难理解。
    【解决方案2】:

    loalStorage 将持续到客户端的缓存被清除,您可以非常轻松地设置或获取该值。

     // Store
     localStorage.setItem("lastname", "Smith");
     // Retrieve
     document.getElementById("result").innerHTML = localStorage.getItem("lastname");
    

    来自Local Storage的代码

    【讨论】:

    • 请。我在最后
    • 将其添加到点击按钮事件中:function swapStyleSheet(sheet){ document.getElementById('design').setAttribute('href', sheet); } 并在脚本的开头设置此 href
    • 你的意思是function swapStyleSheet(sheet){ document.getElementById('design').setAttribute('href', sheet); // Store localStorage.setItem("lastname", "Smith"); // Retrieve document.getElementById("result").innerHTML = localStorage.getItem("lastname"); }
    • 不,document.getElementById('design').setAttribute('href', sheet); // will set the href to your tag localStorage.setItem("sheet", sheet); // will store the value of the current sheet in local storage 除了脚本开头的那个document.getElementById('design').setAttribute('href', localStorage.getItem("sheet")); // 将检索并设置存储在本地存储中的当前值
    猜你喜欢
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 2018-11-05
    相关资源
    最近更新 更多