MDUI文件
複製 llms.txt 連結複製 llms-full.txt 連結以 Markdown 格式檢視此頁與 ChatGPT 討論此頁內容與 ChatGPT 討論專案完整文件
預設顏色
自選顏色
從桌布擷取顏色
請選擇一張桌布
開發指南
AI 輔助開發
樣式
與框架整合
元件
函式
jq 工具庫 dialog alert confirm prompt snackbar getTheme setTheme getColorFromImage setColorScheme removeColorScheme loadLocale setLocale getLocale throttle observeResize breakpoint
獨立程式庫

setTheme

setTheme 函式用來設定目前頁面或指定元素的主題。

可選的主題包括 lightdarkauto。詳見 深色模式

使用方式

按需引入函式:

import { setTheme } from 'mdui/functions/setTheme.js';

使用範例:

// 把整個頁面設定成深色模式
setTheme('dark');

// 把 class="element" 元素設定成深色模式
setTheme('dark', '.element');

// 把指定 DOM 元素設定成深色模式
const element = document.querySelector('.element');
setTheme('dark', element);

API

setTheme(theme: 'light' | 'dark' | 'auto', target?: string | HTMLElement | JQ<HTMLElement>): void

函式的第一個參數是要設定的主題,可選值為 lightdarkauto

第二個參數是要設定主題的元素,可以是 CSS 選擇器、DOM 元素,或 JQ 物件。如果不傳入第二個參數,那麼預設設定 <html> 元素的主題。

本頁目錄