javascript - 单击时激活按钮切换类
问题描述
我有这个按钮
<ChartButton className={this.state.GPUClicked === 0 ? ' toggled' : ''}onClick={() => this.handleGPUChart(0, 'GPU 1')}>GPU 1</ChartButton>
单击时会触发此事件
handleGPUChart = (num, title) => {
this.setState({
GPU1: num,
GPUTitle: title,
GPUClicked: num
})
}
这是此按钮的 CSS
export const ChartButton = styled.button`
background-color: #5BB081;
border: none;
outline: none;
color: white;
padding: 5px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 14px;
margin: 4px 2px;
white-space: nowrap;
cursor: pointer;
.toggled {
background-color: #636863;
font-size: 15px;
}
`
我在这里想念什么?我希望按钮在单击时切换为切换,我认为我在 ChartButton 组件中作为 className 所拥有的内容是有意义的。我正在为此扯掉我的头发......提前非常感谢你。
解决方案
很确定你只需要在你的额外课程之前添加一个 & :
export const ChartButton = styled.button`
background-color: #5BB081;
border: none;
outline: none;
color: white;
padding: 5px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 14px;
margin: 4px 2px;
white-space: nowrap;
cursor: pointer;
&.toggled {
background-color: #636863;
font-size: 15px;
}
`
推荐阅读
- angular - Angular Material - 添加后选择新选项卡 - 事先没有选项卡时出现问题
- javascript - 使用 pycparser 从 C 解析到 Javascript
- python - 熊猫重新采样 .ffill()
- constructor - 模块或名称空间未定义错误 FS0039
- python - 将 pandas 列从字符串 Quarters 和 Years 数组转换为 datetime 列
- python - 路径查找算法练习和使用 .txt 文件
- django - 如何使用现有列在表中添加外键而不会丢失带有 sqlite3 的 django 中的数据?
- python - 将原始值是来自另一个数据帧的列的数据帧中的行数据替换为来自第二个数据帧的值
- flutter - 警报对话框无限运行
- javascript - 如何将共享一个公共键的两个对象的两个值合并在一起并返回它?