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

如何修改SweetAlert2取消按钮文本颜色?自定义类方案失效求助

修改SweetAlert2取消按钮文本颜色的正确方法

你代码里的问题是customClass的键名错误,SweetAlert2中取消按钮对应的customClass键是cancelButton,不是cancelButtonText,这导致自定义类没有绑定到正确的按钮元素上,所以样式不生效。

修改后的JS代码:

return Swal.fire({
  text: label,
  customClass: {
    cancelButton: 'custom-cancel-button',
  },
  showCancelButton: true,
  confirmButtonText: 'Yes',
  cancelButtonText: 'No',
  confirmButtonColor: '#FFF000',
  reverseButtons: true,
})

对应的CSS代码(如果默认样式优先级高,可加!important确保覆盖):

.custom-cancel-button {
  color: #F04249 !important;
}

或者用更具体的选择器避免!important:

.swal2-cancel.custom-cancel-button {
  color: #F04249;
}

内容的提问来源于stack exchange,提问作者Landtel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 16:40:41