【问题标题】:How do I create 3 buttons that change the webpage background to red, green, or blue on-click in HTML?如何创建 3 个按钮,将网页背景更改为 HTML 中的红色、绿色或蓝色单击按钮?
【发布时间】: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"; 如果你想改变整个网页的背景颜色,那么你必须将背景颜色设置为&lt;body&gt;而不是&lt;h1&gt;

标签: html


【解决方案1】:

我们首先在表单标签内创建三个名为 red、green、blue 的单选按钮,并使用名为 on Click 的属性并分配值文档。 bgcolor=颜色名称。

【讨论】:

    猜你喜欢
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    • 2016-08-27
    • 2014-10-09
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多