![](/img/trans.png)
[英]How can I change a CSS file permanently so the changes are saved in the actual file on the server?
[英]How can I permanently change css file in my website using javascript?
我正在尝试向我的网站添加浅色/深色样式功能。 我编写了一个代码,当单击一个按钮时,可以在两个样式表之间进行切换,但是每当我转到其他路线时,样式就会恢复为常规样式。
Javascript:
function swapStyleSheet(sheet){
document.getElementById('pageStyle').setAttribute('href', sheet);
}
HTML:
<link rel="stylesheet" href="/stylesheets/light.css" id="pageStyle">
<body>
<li><a id="navLinks" onclick="swapStyleSheet('/stylesheets/light.css')">Light Style!</a></li>
<li><a id="navLinks" onclick="swapStyleSheet('/stylesheets/main.css')">Dark Style!</a></li>
</body>
这类似于使用cookie,但是数据存储在浏览器中而不是服务器中。 所有现代浏览器均支持。
JS:
function swapStyleSheet(sheet){
document.getElementById('pageStyle').setAttribute('href', sheet);
window.localStorage.setItem("pageStyle", sheet);
}
window.onload = function() {
swapStyleSheet(window.localStorage.getItem("pageStyle"));
}
HTML:
<li><a id="navLinks" onclick="swapStyleSheet('/stylesheets/light.css')">Light Style!</a></li>
<li><a id="navLinks" onclick="swapStyleSheet('/stylesheets/main.css')">Dark Style!</a></li>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.