当切换开关时,我从浏览器控制台得到这些错误。由于某些原因,它不工作,我无法解决为什么这不能工作,我只是刚刚开始Javascript,所以不要我生气,如果我做了一些愚蠢的事情。
Uncaught TypeError: Cannot read properties of undefined (reading 'remove')
at getValue (darkModeSwitch.js:8)
at window.onload (index.html:17)
Uncaught TypeError: Cannot read properties of undefined (reading 'add')
at getValue (darkModeSwitch.js:13)
at HTMLInputElement.onclick (index.html:26)
const body = document.body
const anchor = document.getElementsByTagName("a");
const darkModeToggle = document.getElementById('darkModeToggle');
function getValue(){
if (darkModeToggle.checked) {
body.classList.remove("lightmode")
anchor.classList.remove("lightmode")
console.log("checked")
} else{
console.log("not checked")
body.classList.add("lightmode")
anchor.classList.add("lightmode")
}
};.lightmode{
background-color: white;
color: black;
}<div>
<ul>
<li><a href="kapcsolat/kapcsolat.html?version=691">Kapcsolat</a></li>
<li><a href="projects/projects.html?version=691">Projektek</li>
<li><a class="active" href="index.html?version=692">Kezdőlap</a></li>
<li><label class="switch"><input onclick="getValue()" id="darkModeToggle" type="checkbox" checked><span class="slider round"></span></label><script src="scripts/darkModeSwitch.js"></script>
</li>
<h1 class="jayden" style="float: left;"><a class="jayden" href="index.html?version=-23424893215">jayden.hu</a></h1>
</ul>
</div>
发布于 2021-12-17 21:22:12
错误是因为您试图一次修改多个元素上的classList;您的anchor变量包含一个HTML (所有锚)的数组,而不是一个特定的元素。
谢天谢地,由于CSS级联,除了身体之外,您不需要更改任何元素上的类。只需在CSS中这样处理a元素,就可以继承body的颜色:
function getValue() {
document.body.classList.toggle("lightmode")
};body {
background-color: black;
color: white;
}
.lightmode {
background-color: white;
color: black;
}
a {
color: inherit;
}<body>
<ul>
<li><a href="kapcsolat/kapcsolat.html?version=691">Kapcsolat</a></li>
<li><a href="projects/projects.html?version=691">Projektek</a></li>
<li><a class="active" href="index.html?version=692">Kezdőlap</a></li>
<li><label class="switch"><input onclick="getValue()" id="darkModeToggle" type="checkbox" checked><span class="slider round"></span></label></li>
<h1 class="jayden" style="float: left;"><a class="jayden" href="index.html?version=-23424893215">jayden.hu</a></h1>
</ul>
</body>
发布于 2021-12-17 21:24:25
问题不在于身体,而在于anchor。您使用getElementsByTagName,它返回一个列表(数组),而不是一个元素,即使您有一个a,anchor常量值也将是一个长度为1的数组。
要么使用querySelector,要么按ID选择第一个条目,document.getElementsByTagName("a")[0],->,而不是真正的最佳实践。
或者,如果您有多个锚标记,只需遍历它们。
见下文
const body = document.body
const anchor = document.querySelectorAll("a.jayden");
const darkModeToggle = document.getElementById('darkModeToggle');
function getValue() {
if (darkModeToggle.checked) {
body.classList.remove("lightmode")
anchor.forEach(a => a.classList.remove("lightmode"))
console.log("checked")
} else {
console.log("not checked")
body.classList.add("lightmode")
anchor.forEach(a => a.classList.add("lightmode"))
}
};.lightmode {
background:blue
}
a.lightmode {
color: white
}<input onclick="getValue()" id="darkModeToggle" type="checkbox" checked />
<a class="jayden" href="#" >jayden.hu</a>
<a class="jayden" href="#" >jayden.hu</a>
发布于 2021-12-17 21:54:40
锚是一个HTML集合,应该通过:
const body = document.body;
const anchor = document.getElementsByTagName('a');
const darkModeToggle = document.getElementById('darkModeToggle');
function getValue(){
if (darkModeToggle.checked) {
console.log("checked");
if(body.classList.contains("lightmode")){
body.classList.remove("lightmode")
}
for(var a=0;a<anchor.length;a++){
if(anchor[a].classList.contains("lightmode")){
anchor[a].classList.remove("lightmode");
}
}
} else{
console.log("not checked");
if(!body.classList.contains("lightmode")){
body.classList.add("lightmode")
}
for(var a=0;a<anchor.length;a++){
if(!anchor[a].classList.contains("lightmode")){
anchor[a].classList.add("lightmode");
}
}
}
} .lightmode{
background-color: white;
color: black;
}<body>
<div>
<ul>
<li><a href="kapcsolat/kapcsolat.html?version=691">Kapcsolat</a></li>
<li><a href="projects/projects.html?version=691">Projektek</a></li>
<li><a class="active" href="index.html?version=692">Kezdőlap</a></li>
<li><label class="switch"><input onclick="getValue()" id="darkModeToggle" type="checkbox" checked><span class="slider round"></span></label><script src="scripts/darkModeSwitch.js"></script>
</li>
<h1 class="jayden" style="float: left;"><a class="jayden" href="index.html?version=-23424893215">jayden.hu</a></h1>
</ul>
</div>
</body>
https://stackoverflow.com/questions/70398971
复制相似问题