Сергей Ракипов,
с таким html сложно css применить, но делегирование снова рулит.
<!doctype html>
<html lang="ru">
<head>
<meta charset="utf-8">
<title>index</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="index">
<meta name="Keywords" content="index">
<style>
.blok{
width: 400px;
height: 100px;
margin: 10px auto;
display: flex;
justify-content: space-around;
}
.kvadrat{
width: 100px;
height: 100px;
border: 3px solid #000;
}
.kvadrat.aktiv{
border: 3px solid #0504aa;
}
.kvadrat:focus{
outline: none;
}
.blok h1.hover{
color:#0504aa;
}
</style>
</head>
<body>
<div class="blok">
<h1 class="zagolovok">Заголовок</h1>
<button class="kvadrat aktiv"></button>
<button class="kvadrat"></button>
<button class="kvadrat"></button>
</div>
<div class="blok">
<h1 class="zagolovok">Заголовок</h1>
<button class="kvadrat aktiv"></button>
<button class="kvadrat"></button>
<button class="kvadrat"></button>
</div>
<div class="blok">
<h1 class="zagolovok">Заголовок</h1>
<button class="kvadrat aktiv"></button>
<button class="kvadrat"></button>
<button class="kvadrat"></button>
</div>
<script>
let body = document.body;
let aktiv = ({target}) => {
target = target.closest(".kvadrat");
if(!target) return;
let parent = target.closest(".blok");
let current = parent.querySelector(".kvadrat.aktiv");
current && current.classList.remove("aktiv");
target.classList.add("aktiv");
}
body.addEventListener("click", aktiv);
let noverEvent = {
handleEvent: event => {
target = event.target.closest(".kvadrat");
if(!target) return;
let parent = target.closest(".blok");
let h = parent.querySelector("h1");
h.classList.toggle("hover", event.type == "mouseenter")
}
}
body.addEventListener("mouseenter", noverEvent, true);
body.addEventListener("mouseleave", noverEvent, true);
</script> </body>
</html>