Вам необходимо заменить backgroundColour
на backgroundColor
без u
:
document.getElementById("shapes").style.backgroundColour = colour;
______________________________________________________^
Должно быть:
document.getElementById("shapes").style.backgroundColor = colour;
ПРИМЕЧАНИЕ 1: Вынеобходимо вызвать функцию, чтобы увидеть эффект, и вы также должны дать своему целевому div shapes
a width/height
, чтобы вы могли видеть его.
ПРИМЕЧАНИЕ 2: Вы должны слушать DOMContentLoaded
событие, чтобы убедиться, что все элементы загружены в DOM перед вызовом вашего скрипта.
Рабочий пример:
<!DOCTYPE html>
<html>
<head>
<link href="main.css" rel="stylesheet" type="text/css" />
<style>
#shapes {
width: 100px;
height: 100px;
border: 1px solid #000;
}
</style>
</head>
<body>
<h1>Test Your Reactions!</h1>
<p>Click on the shapes as fast as you can</p>
<div id="shapes">
</div>
<script>
document.addEventListener("DOMContentLoaded", function(event) {
var shapes = document.getElementById("shapes");
shapes.addEventListener('click', myFunction, false);
});
function myFunction() {
shapes.style.backgroundColor = "#" + (Math.random() * (1 << 24) | 0).toString(16).slice(-6);
}
</script>
</body>
</html>