【发布时间】:2019-09-02 02:35:54
【问题描述】:
我正在尝试在我的网页上使用深色主题和浅色主题,但我找不到仅通过一个按钮来切换它们的方法(因此第一次单击按钮会打开深色主题,第二次会打开它关了)。我希望能够在不使用第三方 JavaScript 库的情况下做到这一点。我找到了一种使用<option> 元素的方法,但这不是我想要的“切换按钮”:
<button onclick=getTheme() id=themeToggle>Click to use this theme.</button>
<select id="select">
<option>Dark Theme</option>
<option>Revert To Original</option>
</select>
和
function getTheme () {
function changeTheme (Theme) {
document.getElementById('style').setAttribute('href', Theme);
}
var index = document.getElementById("select").selectedIndex;
switch (index) {
case 0:
changeTheme('css/dark.css');
break;
case 1: changeTheme('css/main.css');
}
}
感谢您的帮助!
【问题讨论】:
-
@Rob “我已经找到了一种方法……” - TO 要求一个按钮在两种状态之间切换
-
使用闭包,全局变量,检查
href属性的值,...
标签: javascript html css