gpt4 book ai didi

javascript - 没有抛出错误,但我仍然无法为包含 RGB 字符串的变量分配 backgroundColor

转载 作者:太空宇宙 更新时间:2023-11-03 22:22:06 25 4
gpt4 key购买 nike

所以我正在尝试制作一个简单的随机颜色猜谜游戏来练习 DOM 操作。我有一个随机数生成器,可以为我提供 0-255 之间的红色、绿色和蓝色值,然后将它们分配给一个名为 randomColor 的变量,该变量的值是一个包含随机红色、绿色和蓝色值的串联字符串。

我似乎无法解决的问题是为什么将 randomColor 变量指定为 HTML 中正方形的 backgroundColor 属性不生效。控制台不记录任何错误,但如果我 console.log 正方形 backgroundColor 它始终是一个空字符串。如何将方 block 的 backgroundColor 重新分配给 randomColor 中保存的 RGB 字符串?

var squares = document.querySelectorAll(".square");

function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}

for (var i=0; i<squares.length; i++) {
let red = getRandomInt(256);
let green = getRandomInt(256);
let blue = getRandomInt(256);
let randomColor = "rgb("+red+", "+green+", "+blue+");";
squares[i].style.backgroundColor = randomColor;
}
html {
box-sizing: border-box;
}

body {
background-color: #222222;
margin: 0;
padding: 0;
}

h2 {
margin: 2% 0;
}

.container {
max-width: 80%;
margin: 0 auto;
display: flex;
flex-wrap: wrap;
}

.top {
background-color: white;
margin-top: 0;
padding-top: 0;
}

.stripe {
padding: 0;
background-color: steelblue;
color: white;
}

.stripe p {
margin: 0 auto;
padding-top: 3%;
}

.square {
display: inline-block;
width: 30%;
padding-bottom: 33%;
margin: 2px;
border-radius: 5%;
background-color: magenta;
}

.heading {
font-size: 2em;
margin-top: 0;
padding: 2% 0;
}

.text-center {
text-align: center;
}
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>The Great RGB Guessing Game</title>
<link rel="stylesheet" href="syles.css" type="text/css" />
</head>
<body>
<div class="top">
<h1 class="heading text-center">The Great<br><span>Color</span><br>Guessing Game</h1>
<div class="stripe">
<div class="container">
<h2>Reset</h2>
<p>Try again</p>
<h2><span>Easy</span> | <span>Hard</span></h2>
</div><!-- /.container -->
</div><!-- /.stripe -->
</div>
<div class="container">
<div class="square" id="square1"></div>
<div class="square" id="square2"></div>
<div class="square" id="square3"></div>
<div class="square" id="square4"></div>
<div class="square" id="square5"></div>
<div class="square" id="square6"></div>
</div>
<script type="text/javascript" src="color-game.js"></script>
</body>
</html>

最佳答案

问题是 backgroundColor 值末尾的分号 (;)。

因此,使用 "rgb("+red+", "+green+"而不是 "rgb("+red+", "+green+", "+blue+");" , "+blue+")"

如果删除它,JS 应该可以正常工作。

var squares = document.querySelectorAll(".square");

function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}

for (var i=0; i<squares.length; i++) {
let red = getRandomInt(256);
let green = getRandomInt(256);
let blue = getRandomInt(256);
let randomColor = "rgb("+red+", "+green+", "+blue+")";
squares[i].style.backgroundColor = randomColor;
}
html {
box-sizing: border-box;
}

body {
background-color: #222222;
margin: 0;
padding: 0;
}

h2 {
margin: 2% 0;
}

.container {
max-width: 80%;
margin: 0 auto;
display: flex;
flex-wrap: wrap;
}

.top {
background-color: white;
margin-top: 0;
padding-top: 0;
}

.stripe {
padding: 0;
background-color: steelblue;
color: white;
}

.stripe p {
margin: 0 auto;
padding-top: 3%;
}

.square {
display: inline-block;
width: 30%;
padding-bottom: 33%;
margin: 2px;
border-radius: 5%;
background-color: magenta;
}

.heading {
font-size: 2em;
margin-top: 0;
padding: 2% 0;
}

.text-center {
text-align: center;
}
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>The Great RGB Guessing Game</title>
<link rel="stylesheet" href="syles.css" type="text/css" />
</head>
<body>
<div class="top">
<h1 class="heading text-center">The Great<br><span>Color</span><br>Guessing Game</h1>
<div class="stripe">
<div class="container">
<h2>Reset</h2>
<p>Try again</p>
<h2><span>Easy</span> | <span>Hard</span></h2>
</div><!-- /.container -->
</div><!-- /.stripe -->
</div>
<div class="container">
<div class="square" id="square1"></div>
<div class="square" id="square2"></div>
<div class="square" id="square3"></div>
<div class="square" id="square4"></div>
<div class="square" id="square5"></div>
<div class="square" id="square6"></div>
</div>
<script type="text/javascript" src="color-game.js"></script>
</body>
</html>

关于javascript - 没有抛出错误,但我仍然无法为包含 RGB 字符串的变量分配 backgroundColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53106351/

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