Django实现明暗主题切换遇静态文件JS空值错误求助
问题分析与解决方案
你遇到的Uncaught TypeError: Cannot read properties of null (reading 'querySelector')报错,核心原因是JS代码尝试对不存在的DOM元素调用querySelector方法,尽管功能能运行,但局部逻辑存在空值未判断的问题,迁移到Django模板后触发了报错。结合你的代码,具体修复方案如下:
1. 修复JS中的空值判断与变量冲突
你的JS代码中存在变量名重复覆盖、未判断DOM元素是否存在的问题,导致找不到对应元素时触发报错。修改后的完整JS代码如下:
}(jQuery), APP = APP || {}, function ($) { 'use strict'; APP.colorModes = { init: function () { this.colorModes(); }, colorModes: function () { if (!document.documentElement.hasAttribute("data-bs-theme")) return; // 读取并验证本地存储的主题值,确保合法 const savedTheme = localStorage.getItem("theme"); const getCurrentTheme = () => { if (savedTheme && ["light", "dark"].includes(savedTheme)) { return savedTheme; } return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"; }; const setTheme = (theme) => { if (theme === "auto") { document.documentElement.setAttribute("data-bs-theme", window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"); } else { document.documentElement.setAttribute("data-bs-theme", theme); } }; setTheme(getCurrentTheme()); // 更新主题切换按钮状态的函数,添加空值判断 const updateThemeButtons = (themeValue, shouldFocus = false) => { const colorModesContainer = document.querySelector(".color-modes"); if (!colorModesContainer) return; // 移除所有按钮的激活状态 document.querySelectorAll("[data-bs-theme-value]").forEach(btn => { btn.classList.remove("active"); btn.setAttribute("aria-pressed", "false"); }); document.querySelectorAll(".color-modes").forEach(container => { const activeIconUse = container.querySelector(".theme-icon-active use"); const themeBtn = container.querySelector(`[data-bs-theme-value="${themeValue}"]`); // 关键:判断按钮是否存在,避免空值调用querySelector if (!themeBtn || !activeIconUse) return; const iconHref = themeBtn.querySelector("svg use").getAttribute("href"); themeBtn.classList.add("active"); themeBtn.setAttribute("aria-pressed", "true"); activeIconUse.setAttribute("href", iconHref); }); if (shouldFocus) colorModesContainer.focus(); }; // 监听系统主题偏好变化 window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", () => { if (savedTheme !== "light" && savedTheme !== "dark") { setTheme(getCurrentTheme()); updateThemeButtons(getCurrentTheme()); } }); // 绑定按钮点击事件 const bindThemeEvents = () => { updateThemeButtons(getCurrentTheme()); document.querySelectorAll("[data-bs-theme-value]").forEach(btn => { btn.addEventListener("click", () => { const theme = btn.getAttribute("data-bs-theme-value"); localStorage.setItem("theme", theme); setTheme(theme); updateThemeButtons(theme, true); }); }); }; bindThemeEvents(); } }; $(document).ready(function () { APP.colorModes.init(); }); }
2. 确保Django模板中SVG图标资源正确加载
你的HTML中使用了SVG sprite(如#sun-fill),如果在Django模板中未正确引入对应的SVG资源,可能导致元素识别异常。需要在模板中添加SVG sprite定义:
{% load static %} <!-- 隐藏的SVG Sprite容器,存放所有需要的图标 --> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"> <symbol id="sun-fill" viewBox="0 0 16 16"> <path d="M8 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM8 0a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 0zm0 13a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 13zm8-5a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2a.5.5 0 0 1 .5.5zM3 8a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2A.5.5 0 0 1 3 8zm10.657-5.657a.5.5 0 0 1 0 .707l-1.414 1.415a.5.5 0 1 1-.707-.708l1.414-1.414a.5.5 0 0 1 .707 0zm-9.193 9.193a.5.5 0 0 1 0 .707L1.05 13.657a.5.5 0 0 1-.707-.707l1.414-1.414a.5.5 0 0 1 .707 0zm9.193 2.121a.5.5 0 0 1-.707 0l-1.414-1.414a.5.5 0 0 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .707zM1.05 2.343a.5.5 0 0 1 .707 0l1.414 1.414a.5.5 0 1 1-.707.707L1.05 3.05a.5.5 0 0 1 0-.707z"/> </symbol> <symbol id="moon-stars-fill" viewBox="0 0 16 16"> <path d="M6 .278a.768.768 0 0 1 .08.858 7.208 7.208 0 0 0-.878 3.46c0 4.021 3.278 7.277 7.318 7.277.527 0 1.04-.055 1.533-.16a.787.787 0 0 1 .81.316.733.733 0 0 1-.031.893A8.349 8.349 0 0 1 8.344 16C3.734 16 0 12.286 0 7.71 0 4.266 2.114 1.312 5.124.06A.752.752 0 0 1 6 .278z"/> <path d="M10.794 3.148a.217.217 0 0 1 .412 0l.387 1.162c.173.518.579.924 1.097 1.097l1.162.387a.217.217 0 0 1 0 .412l-1.162.387a1.734 1.734 0 0 0-1.097 1.097l-.387 1.162a.217.217 0 0 1-.412 0l-.387-1.162A1.734 1.734 0 0 0 9.31 6.593l-1.162-.387a.217.217 0 0 1 0-.412l1.162-.387a1.734 1.734 0 0 0 1.097-1.097l.387-1.162zM13.863.099a.145.145 0 0 1 .274 0l.258.774c.115.346.386.617.732.732l.774.258a.145.145 0 0 1 0 .274l-.774.258a1.156 1.156 0 0 0-.732.732l-.258.774a.145.145 0 0 1-.274 0l-.258-.774a1.156 1.156 0 0 0-.732-.732l-.774-.258a.145.145 0 0 1 0-.274l.774-.258c.346-.115.617-.386.732-.732L13.863.1z"/> </symbol> <symbol id="check2" viewBox="0 0 16 16"> <path d="M13.854 3.646a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L6.5 10.293l6.646-6.647a.5.5 0 0 1 .708 0z"/> </symbol> </svg>
3. 确认Django静态资源加载顺序
确保JS文件在DOM渲染完成后加载:将JS引用放在模板的</body>标签前,或者通过{% block js %}在基础模板的底部引入,避免JS在DOM元素生成前执行。
{% load static %} <!-- 页面内容 --> <script src="{% static 'path/to/your/jquery.js' %}"></script> <script src="{% static 'path/to/your/theme-switch.js' %}"></script> </body>
内容的提问来源于stack exchange,提问作者Emad Younan
相关产品推荐
相关产品推荐

