Skip to main content
Stack Overflow на русском

Вернуться к просмотру редакций

1 из 2
SwaD
  • 11.8k
  • 9
  • 17
  • 32

Попробуйте исправить свой код вот так:

const button = document.getElementById("btn");
const color = document.querySelector(".color");
button.addEventListener("click", () => {
 let hexColor = generateHex();
 document.body.style.backgroundColor = hexColor[1];
 color.textContent = hexColor[0];
});
function generateHex() {
 const newObject = {
 Aqua: "#00ffff",
 Gray: "#808080",
 Navy: "#000080",
 Green: "#008000", 
 Olive: "#808000" 
 };
 const colorArr = Object.entries(newObject);
 return colorArr[Math.round(Math.random() * (colorArr.length - 1))];
}
<div class="color"></div>
<button id='btn'>Жмяк</button>

SwaD
  • 11.8k
  • 9
  • 17
  • 32
default

AltStyle によって変換されたページ (->オリジナル) /