【发布时间】:2021-02-10 13:08:28
【问题描述】:
所以,我马上就知道,为暗模式使用单独的样式表已经是一种奇怪的方式,但这是我的情况:
我有一个输入触发器,当 .prop("checked", true) 时,将 darkmode.css 切换到 styles.css 上,当 .prop("checked", false) 时,将其删除,您只需标准的“灯光模式”样式。
我想要实现的是,如果主页上有 .prop("checked", true),然后我点击另一个页面,例如“关于我们”,我想保持那个 .prop("checked" , true) 并保持暗模式。
<input type="checkbox" id="darkmodeToggle" class="darkmode"></input>
$("#darkmodeToggle").click(function() {
if ($("#darkmodeToggle").prop("checked", true)) {
$("<link>").attr({'rel': 'stylesheet', 'href': 'styles/darkmode.css'}).appendTo('body');
} else if ($("#darkmodeToggle").prop("checked", false)) {
$("<link>").attr({'rel': 'stylesheet', 'href': 'styles/styles.css'}).appendTo('body');
$("<link>").attr({'rel': 'stylesheet', 'href': 'styles/about.styles.css'}).appendTo('body');
$("<link>").attr({'rel': 'stylesheet', 'href': 'styles/operations.styles.css'}).appendTo('body');
}});
【问题讨论】:
-
可以通过替换href来切换!!不附加它!
标签: html jquery hyperlink stylesheet attr