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

如何在Tailwind中为非.disabled状态的按钮添加cursor-pointer类?

纯Tailwind实现方案

直接利用Tailwind的否定伪类变体,就能实现仅在按钮未添加.disabled类时应用cursor-pointer样式:

<button class="prev not-[.disabled]:cursor-pointer ...">上一页</button>
<button class="next not-[.disabled]:cursor-pointer ...">下一页</button>

not-[.disabled]是Tailwind的自定义否定变体,会匹配所有不包含.disabled类的元素,只有满足这个条件时,cursor-pointer才会生效。

如果你的项目需要频繁用到这类逻辑,也可以在tailwind.config.js中注册自定义变体,让写法更简洁:

/** @type {import('tailwindcss').Config} */
module.exports = {
  // ...其他配置项
  plugins: [
    function({ addVariant }) {
      addVariant('not-disabled', '&:not(.disabled)');
    }
  ]
}

注册完成后就可以这样使用:

<button class="prev not-disabled:cursor-pointer ...">上一页</button>
<button class="next not-disabled:cursor-pointer ...">下一页</button>

以上两种方式都无需编写原生CSS,完全基于Tailwind实现需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 19:17:02