我制作了一个JS文件,可将静态HTML文件转换为深色版本。现在,我试图将其与ReactJS一起使用,但出现了“预期分配或函数调用,而是看到一个表达式no-unused-expressions”的错误。
这是我的代码
moduleDirectories: ["node_modules", "src"],
错误是:“意外使用'addEventListener'no-restricted-globals”
第11行是://Check if the local storage has the dark mode or light mode prefence.
document.addEventListener('DOMContentLoaded', (event) => {
((localStorage.getItem('mode') || 'dark') === 'dark') ?
document.querySelector('body').classList.add('dark') :
document.querySelector('body').classList.remove('dark')
});
//Get the element and add the dark class to them.
document.getElementById("moon"); addEventListener("click", e => {
localStorage.setItem('mode', (localStorage.getItem('mode') || 'dark') === 'dark' ? 'light' : 'dark')
const elements = document.querySelectorAll("body, hr");
elements.forEach(el => {
if (localStorage.getItem('mode') === 'dark') {
el.classList.add('dark')
} else {
el.classList.remove('dark')
}
})
});