[英]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 + ")";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.