我如何在這個函數中定位其他元素(除了body)?這樣做有可能嗎?
因此,該功能允許我使用按鈕切換暗/亮主題,與body元素一起工作很好,但如果我試圖包含任何其他元素,與body包含的方式相同,它不適用。
(function() {
let onpageLoad = localStorage.getItem("tema") || "";
let element = document.body;
element.classList.add(onpageLoad);
document.getElementById("tema").textContent =
localStorage.getItem("tema") || "light";
})();
function promeniTemu() {
let element = document.body;
element.classList.toggle("svetlaTema");
let tema = localStorage.getItem("tema");
if (tema && tema === "svetlaTema") {
localStorage.setItem("tema", "");
} else {
localStorage.setItem("tema", "svetlaTema");
}
document.getElementById("tema").textContent = localStorage.getItem("tema");
}```
querySelector seems to work, but if I use it for example on a header, the theme doesn't save on other pages, it refreshes, but if its only the body being changed theme works normally and saves everytime.
使用querySelector代替getElementById是可行的。
下一篇調查頁面間距錯誤