From 05cc13a0a874676ad35f674358569d05c15fc023 Mon Sep 17 00:00:00 2001 From: hehh Date: Tue, 25 Nov 2025 10:59:59 +0800 Subject: [PATCH] =?UTF-8?q?refactor(js):=20=E4=BC=98=E5=8C=96=E4=B8=BB?= =?UTF-8?q?=E9=A2=98=E5=88=87=E6=8D=A2=E6=8C=89=E9=92=AE=E7=9A=84jQuery?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E5=99=A8=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将重复的$('#theme-btn')选择器调用提取为常量themeBtn - 提高代码可读性和维护性 - 避免重复查询DOM元素提升性能 --- js/about.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/js/about.js b/js/about.js index 64640e3..a0c7025 100644 --- a/js/about.js +++ b/js/about.js @@ -184,21 +184,22 @@ class ThemeManager { init() { let theme = getStoredTheme(); if (theme) this.root.setAttribute('data-theme', theme); - $('#theme-btn').toggleClass('is-active', theme === 'night'); + const themeBtn = $('#theme-btn'); + themeBtn.toggleClass('is-active', theme === 'night'); const langForTitle = getStoredLanguage(); const titleText = theme === 'night' ? (langForTitle === 'zh' ? '白天模式' : 'Day') : (langForTitle === 'zh' ? '黑夜模式' : 'Night'); - $('#theme-btn').attr('title', titleText); + themeBtn.attr('title', titleText); - $('#theme-btn').on('click', () => { + themeBtn.on('click', () => { const curr = this.root.getAttribute('data-theme'); const next = curr === 'night' ? 'day' : 'night'; if (next) this.root.setAttribute('data-theme', next); else this.root.removeAttribute('data-theme'); setStoredTheme(next) - $('#theme-btn').toggleClass('is-active', next === 'night'); + themeBtn.toggleClass('is-active', next === 'night'); const lang = getStoredLanguage(); const t = next === 'night' ? (lang === 'zh' ? '白天模式' : 'Day') : (lang === 'zh' ? '黑夜模式' : 'Night'); - $('#theme-btn').attr('title', t); + themeBtn.attr('title', t); // 更新Artalk主题 if (typeof Artalk !== 'undefined') {