You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击按钮时.is-toggled样式不生效的问题求助

点击按钮时.is-toggled样式不生效的问题求助

大家好,我碰到个小问题想请教下各位:我给按钮加了点击切换.is-toggled类的逻辑,现在能确认点击时这个类确实被正确添加/移除了,但对应的样式就是不生效,按钮外观完全没变化,这可愁坏我了😅

以下是我目前写的代码:

CSS 代码

.is-toggled {
  background-color: lightgray;
  border: none;
  padding: 10px;
  border-radius: 5px;
  cursor: pointer;
}

.gaming-btn {
  background-color: black;
  color: white;
  border: none;
  padding: 10px;
  border-radius: 5px;
  cursor: pointer;
}

HTML 代码

<button class="gaming-btn" onclick="
     const btn = document.querySelector('.gaming-btn');

      if(btn.classList.contains('is-toggled')) {
        btn.classList.remove('is-toggled')
      } else {
        btn.classList.add('is-toggled');
      }
    ">Gaming</button>

有没有大佬能帮我看看问题出在哪,怎么才能让.is-toggled的样式正常生效呀?

备注:内容来源于stack exchange,提问作者Monish M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 19:29:30