gpt4 book ai didi

javascript - 更改所有页面的 CSS

转载 作者:太空宇宙 更新时间:2023-11-04 07:56:35 29 4
gpt4 key购买 nike

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

<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>

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

最佳答案

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

var stylesheet = localStorage.getItem("stylesheet");

function swapStyleSheet(sheet){
document.getElementById('pagestyle').setAttribute('href', sheet);
localStorage.setItem('stylesheet', sheet);
}

if(stylesheet) {
swapStyleSheet(stylesheet)
}

关于javascript - 更改所有页面的 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47284731/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com