【问题标题】:Changing CSS across all pages更改所有页面的 CSS
【发布时间】:2017-11-14 11:32:53
【问题描述】:

我希望能够在两个不同的样式表之间进行交换,并保持在主页上选择的样式,但在所有页面中保持使用一致。

<link id="pagestyle" rel="stylesheet" href="style1.css">
<script type="text/javascript">
  function swapStyleSheet(sheet){
    document.getElementById('pagestyle').setAttribute('href', sheet);
  }
</script>

<button onclick="swapStyleSheet('style.css')">Blue</button>
<button onclick="swapStyleSheet('style1.css')">White</button>

这只会更改主页的样式表,但我如何将此更改应用到网站中的所有其他页面?

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    您可以使用本地存储来存储当前样式表。

    var stylesheet = localStorage.getItem("stylesheet");
    
    function swapStyleSheet(sheet){
        document.getElementById('pagestyle').setAttribute('href', sheet);
        localStorage.setItem('stylesheet', sheet);
    }
    
    if(stylesheet) {
        swapStyleSheet(stylesheet)
    }
    

    【讨论】:

    • 第一行抛出错误:'the operation is insecure'
    猜你喜欢
    • 1970-01-01
    • 2020-11-15
    • 2013-11-23
    • 1970-01-01
    • 2018-01-01
    • 2013-10-22
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多