源代码:
下载代码
AI 编程工具
点击运行
<!doctype html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://cdn.tailwindcss.com"></script> <script> tailwind.config = { // darkMode: 'media', // 基于系统首选项 (默认) darkMode: 'class', // 基于类名手动切换 theme: { extend: { colors: { brand: { DEFAULT: '#1DA1F2', dark: '#0d74b8', }, }, }, }, plugins: [], }; </script> </head> <body> <button id="toggle-dark-mode" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">切换背景颜色</button> <div class="text-brand dark:text-brand-dark"> 定制深色模式颜色! </div> <script> const toggle = document.getElementById('toggle-dark-mode'); toggle.addEventListener('click', () => { document.documentElement.classList.toggle('dark'); }); </script> </body> </html>
运行结果: