[英]Use classList.toggle on external CSS
So I've been messing around with JS, and I wanted to make a dark theme toggle button as seen here.所以我一直在搞 JS,我想制作一个黑色主题切换按钮,如图所示。
However, that tutorial only shows how to make it work with the <style>
tag in HTML, not using an external CSS file.但是,该教程仅展示了如何使用 HTML 中的
<style>
标签,而不是使用外部 CSS 文件。
How could I make it toggle the class .d_theme
from an external CSS file?我怎样才能
.d_theme
从外部 CSS 文件切换.d_theme
类?
No difference between external and internal CSS when you use JS to toggle使用 JS 切换时,外部和内部 CSS 没有区别
document.getElementById("chk").addEventListener("change",function() { document.getElementById("x").classList.toggle("mystyle",this.checked) })
/* This can be in an external file */ .mystyle { width: 100%; padding: 25px; background-color: coral; color: white; font-size: 25px; box-sizing: border-box; }
<label><input type="checkbox" id="chk">Change theme</label> <div id="x">Here is some text</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.