html - 在没有 JAVASCRIPT 的情况下检查时如何更改 CSS 复选框标签图标
问题描述
我有一个没有 javascript 的汉堡菜单(我不能使用它,这就是分配),使用来自 FontAwesome 的标签图标,我希望在选中复选框时图标更改为另一个,我只是不知道该怎么做那。我在网上查了一下,显然没有 JS 是不可能的,但我宁愿问一下以防万一。
该图标直接在标签内使用class
,我知道我可以添加任意数量的标签,它们只会堆叠起来,但我不知道如何根据复选框的状态隐藏/显示其中一个或者如果有另一种方式:
<div id="hamburger">
<img src="thelogo.png" alt="logo">
<input type="checkbox" id="button">
<label for="button" class="fas fa-bars"></label>
<ul class="items">
<li>EPISODES</li>
<li>INTERVIEWS</li>
<li>ABOUT US</li>
</ul>
</div>
解决方案
您可以使用多个图标并显示/隐藏任何您想要的。
<input type="checkbox" id="button">
<label for="button" class="fas fa-bars"></label>
<label for="button" class="fas arrow-circle-up"></label>
#button:checked ~ .fa-bars {
display: none;
}
#button:checked ~ .arrow-circle-up {
display: inline-block;
}
或者更优雅的方式是更新图标代码的内容。
#button ~ label::before {
content: '\f0c9'; // bars code
}
#button:checked ~ label::before {
content: '\f0aa'; // arrow up code
}
推荐阅读
- javascript - 从突出显示的句子中的一个单词中删除背景颜色
- class - 继承和指向派生类实例的智能指针
- reactjs - 使用 material-kit-react 部署 gatsbyv2 失败
- python - 对 sklearn 中的 random_state 感到困惑
- python - 在 Sanic 路由参数中指定布尔值
- javascript - 如何对多层深的对象强制执行空检查?
- numpy - 有没有办法将在 C 中创建的 numpy Array 强制转换为 MaskedArray?
- python - 拆分字符串多个分隔符
- c# - 打开 CommonOpenFileDialog 后如何防止窗口样式更改
- ssis - 平面文件连接管理器不会在记录不良的文件上失败