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

Flowbite/Tailwind标签页切换时背景色未正常更新问题求助

Flowbite标签页背景色切换问题修复

核心问题分析

  1. 初始激活的Profile标签aria-selected属性错误设为false,Flowbite无法正确识别默认激活项
  2. 未在Tabs容器上配置data-active-classes和data-inactive-classes,导致组件不知道切换时要替换哪些背景色样式
  3. 手动给初始标签绑定了固定背景色类,覆盖了Flowbite的动态样式切换逻辑

修复代码

'use client'
import Image from 'next/image';
import Link from 'next/link';
import React, { useEffect } from 'react';
import { initFlowbite} from 'flowbite';

export default function Winners() { 
    useEffect(() => {
        initFlowbite();
    }, []); 

    return ( 
        <>
            {/* 添加data-active-classes和data-inactive-classes到Tabs容器 */}
            <ul 
                className="flex flex-wrap text-sm font-medium text-center text-gray-500 border-b border-gray-200 dark:border-gray-700 dark:text-gray-400"
                id="default-tab"
                data-tabs-toggle="#default-tab-content" 
                role="tablist"
                data-active-classes="text-blue-600 bg-gray-200 dark:bg-gray-800 dark:text-blue-500"
                data-inactive-classes="text-gray-500 hover:text-gray-600 hover:bg-gray-200 dark:text-gray-400 dark:hover:text-gray-300 dark:hover:bg-gray-800"
            >
                <li className="me-2">
                    <button  
                        aria-current="page" 
                        className="inline-block p-4 rounded-t-lg"
                        id="profileTab" 
                        data-tabs-target="#profile" 
                        type="button" 
                        role="tab" 
                        aria-controls="profile" 
                        aria-selected="true"> {/* 改为true */}
                        Profile
                    </button>
                </li>
                <li className="me-2">
                    <button
                        className="inline-block p-4 rounded-t-lg"
                        id="dashboard-tab" 
                        data-tabs-target="#dashboard" 
                        type="button" 
                        role="tab" 
                        aria-controls="dashboard" 
                        aria-selected="false">
                        Dashboard
                    </button>
                </li>
            </ul>

            <div id="default-tab-content">
                <div className="hidden px-2 md:px-4 rounded-lg bg-white dark:bg-gray-800" 
                        id="profile" 
                        role="tabpanel" 
                        aria-labelledby="profileTab">
                    <p className=" text-gray-700 dark:text-gray-400">
                        this is the profile tab</p>
                </div>
                <div className="hidden px-2 md:px-4 rounded-lg bg-white dark:bg-gray-800" 
                        id="dashboard" 
                        role="tabpanel" 
                        aria-labelledby="dashboard-tab">
                    <p className="text-gray-700 dark:text-gray-400">
                    dashboard sdfsdf sdfsdfs ssdsfsdfsdfsdf</p>
                </div>
            </div>
        </>
    )
}

关键修改说明

  • 在<ul>标签上通过data-active-classes定义激活状态的文本色和背景色,data-inactive-classes定义非激活及hover状态的样式
  • 初始激活的Profile标签aria-selected设为true,移除手动添加的固定样式类,让Flowbite动态控制
  • 所有标签的基础样式统一,仅保留布局类(inline-block p-4 rounded-t-lg),状态类由组件自动切换

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 03:23:28