gpt4 book ai didi

javascript - Uncaught ReferenceError : (function) darkmode is not defined at HTMLButtonElement. onclick

转载 作者:行者123 更新时间:2023-12-02 22:57:27 25 4
gpt4 key购买 nike

尝试制作一个带有深色模式但带有深色模式按钮的歌词网站只会导致错误

我尝试在网上寻找解决方案,但大多数人都说将脚本移到正文标记内,但我的脚本已经在正文中

<DOCTYPE! html>
<html>
<head>
<title>Lyrics Page</title>
</head>
<style>
#bgr {
background-color: white;
}
#lyrics {
color:black;
}
#songtitle {
color: black;
text-align:center;
}
</style>
<body id=bgr>
<script>
function darkmode() {
document.getElementById("bgr").style.background-color = "black";
document.getElementById("songtitle").style.color = "grey";
document.getElementById("lyrics").style.color = "grey";
}
</script>
<button type="button" onclick="darkmode()">Dark Mode</button>
<h1 id=songtitle>song</h1>
<h2 id=songtitle>artist</h2>
<br>
<center id=lyrics>paragraph 1</center>
<br>
<center id=lyrics>paragraph 2</center>
<br>
<center id=lyrics>paragraph 3</center>
</body>
</html>

最佳答案

您的问题是 document.getElementById("bgr").style.background-color = "black"; 部分。在 JavaScript 中设置 css 属性时,您应该使用驼峰命名法。所以如果你改变:

document.getElementById("bgr").style.background-color = "black";

document.getElementById("bgr").style.backgroundColor = "black";

它会起作用。

关于javascript - Uncaught ReferenceError : (function) darkmode is not defined at HTMLButtonElement. onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57927180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com