随机彩色圆圈
Randomly colored circle
我正在做一个小项目,从所有可能的颜色中为这个圆圈分配一种随机颜色。
我试图通过为每个 RGB 值分配一个随机数来实现这一点。但是,它似乎无法以这种方式工作。我也尝试过以 setAttribute() 的方式来分配它,但也没有运气。请看:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Beginning JavaScript</title>
<style>
#circle{
margin:auto;
border:1px solid black;
border-radius: 50%;
background-color:rgb(0,0,0);
height:200px;
width:200px;
}
</style>
</head>
<body>
<div id="circle"></div>
<script>
var circle = document.getElementById('circle');
var value1 = Math.floor(Math.random() * 256);
var value2 = Math.floor(Math.random() * 256);
var value3 = Math.floor(Math.random() * 256);
circle.style.backgroundColor = "rgb(value1,value2,value3)"
您需要将生成的值连接到样式语句中:
circle.style.backgroundColor = "rgb(" + value1 + "," + value2 + "," + value3 + ")";
我正在做一个小项目,从所有可能的颜色中为这个圆圈分配一种随机颜色。
我试图通过为每个 RGB 值分配一个随机数来实现这一点。但是,它似乎无法以这种方式工作。我也尝试过以 setAttribute() 的方式来分配它,但也没有运气。请看:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Beginning JavaScript</title>
<style>
#circle{
margin:auto;
border:1px solid black;
border-radius: 50%;
background-color:rgb(0,0,0);
height:200px;
width:200px;
}
</style>
</head>
<body>
<div id="circle"></div>
<script>
var circle = document.getElementById('circle');
var value1 = Math.floor(Math.random() * 256);
var value2 = Math.floor(Math.random() * 256);
var value3 = Math.floor(Math.random() * 256);
circle.style.backgroundColor = "rgb(value1,value2,value3)"
您需要将生成的值连接到样式语句中:
circle.style.backgroundColor = "rgb(" + value1 + "," + value2 + "," + value3 + ")";