【发布时间】:2022-12-22 07:35:45
【问题描述】:
我正在尝试创建一个网页,其中有三个标记为“红色”、“绿色”和“蓝色”的工作按钮。当用户单击其中一个按钮时,整个网页应更改为单击的特定颜色。
这是我到目前为止所拥有的,但我很确定我做错了什么。
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1 id="H1">H1</h1>
<button type="button" onclick="myFunction()">Set H1 to Red</button>
<script>
function myFunction() {
document.getElementById("H1").style.color = "#ff0000";
}
</script>
</body>
</html>
【问题讨论】:
-
您需要使用
backgroundColor属性。document.getElementById("H1").style.backgroundColor= "#ff0000";如果你想改变整个网页的背景颜色,那么你必须将背景颜色设置为<body>而不是<h1>
标签: html