与本地存储交换样式表

编程入门 行业动态 更新时间:2024-10-26 03:32:28
本文介绍了与本地存储交换样式表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是设计师,JS对我来说完全陌生.刷新后,我想保留所选样式表的本地存储.我能够找到交换样式表的代码,但是不确定使用localstorage添加什么代码

I'm a designer and JS is completely foreign to me. I'd like to keep localstorage of the selected stylesheet after refresh. I was able to find code to swap stylesheet, but I'm not sure what code to add to use localstorage

<!DOCTYPE html> <html> <head> <link id="pagestyle" rel="stylesheet" type="text/css" href="style1.css"> <script> function swapStyleSheet(sheet){ document.getElementById('pagestyle').setAttribute('href', sheet); } </script> </head> <body> <h2>Javascript Change StyleSheet Without Page Reload</h2> <button onclick="swapStyleSheet('style1.css')">Dark Style Sheet</button> <button onclick="swapStyleSheet('style2.css')">Blue Style Sheet</button> <button onclick="swapStyleSheet('style3.css')">Default Style Sheet</button> </body> </html>

推荐答案

您可以使用此代码

<script> var swapStyleSheet = function (sheet) { document.getElementById('pagestyle').setAttribute('href', sheet); storebackground(sheet); } var storebackground = function (swapstylesheet) { localStorage.setItem("sheetKey", swapstylesheet); //you need to give a key and value } var loadbackground = function () { document.getElementById('pagestyle').setAttribute('href', localStorage.getItem('sheetKey')); } window.onload = loadbackground(); </script>

更多推荐

与本地存储交换样式表

本文发布于:2023-11-06 14:40:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563973.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:样式表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!