<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.color {
height: 200px;
width: 200px;
background: #0d0da2;
margin: 5px;
}
#test {
height: 200px;
width: 200px;
background: #0d0da2;
margin: 5px;
}
</style>
</head>
<body>
<div class="color"></div>
<div class="color"></div>
<div id="test"></div>
<script>
var color = document.getElementsByClassName("color");
var color1 = color[0];
color1.onclick = function () {
color1.style.background = (color1.style.background == "green") ? "#0d0da2" : "green";
}
for (var i = 0; i < color.length; i++) {
color[i].onclick = function () {
color[i].style.background = (color[i].style.background == "green") ? "#0d0da2" : "green";
}
}
var test = document.getElementById("test");
test.onclick = function () {
test.style.background = (test.style.background == "green") ? "#0d0da2" : "green";
}
</script>
</body>
</html>
вот хочу попробовать подключить событие к каждому элементу массива. в коде присутствуют три варианта: событие элемента массива, событие элементов массива, и событие объекта (тут все просто, все работает). первые два варианта не работают. буду благодарен, если подскажете почему