0

Нужно по нажатию на кнопку присвоить ей(кнопке) backgroundColor на один из цветов радуги. Я сделал это следующим образом

function buttonRand() {
var color = ["#FF0000","#FFA500","#FFFF00","#008000", "#87CEEB", "#0000FF"," #8A2BE2"];
var button = document.getElementById('buttonRand');
var i = Math.floor(Math.random() * color.length);
button.style.backgroundColor = '"' + color[i] + '"';}

<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div>
    <button id="buttonRand" onclick="buttonRand()">RAINBOW</button>
</div>
<script src="script.js"></script>
</body>
</html>

Но при нажатии на кнопку не происходит никаких действий, даже в консоле нет никаких ошибок. Помогите, пожалуйста.

1 ответ 1

2

Кавычки лишние:

function buttonRand() {
    var color = ["#FF0000","#FFA500","#FFFF00","#008000", "#87CEEB", "#0000FF"," #8A2BE2"];
    var button = document.getElementById('buttonRand');
    var i = Math.floor(Math.random() * color.length);
    button.style.backgroundColor = color[i];
}
<div>
    <button id="buttonRand" onclick="buttonRand()">RAINBOW</button>
</div>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.