Я не думаю, что вы можете сделать это с помощью одного CSS, но вы также отметили javascript, поэтому:
Вы можете сделать это с помощью mouseover
и mouseout
, что соответствует href
; см. комментарии:
// Remove any existing highlight
function removeHighlight() {
document.querySelectorAll(".highlight").forEach(el => {
el.classList.remove("highlight");
});
}
// Add a highlight to the hovered element (or its LI parent, if any), and any
// matching element by `href` attribute (or its LI parent, if any)
function handler(event) {
const a = event.target.closest("a");
if (a && this.contains(a)) {
removeHighlight();
// Note: It's important to use `getAttribute` here, not the reflected
// `href` property, because `getAttribute` will return what's actually
// in the attribute, but `href` will return a fully-resolved link
document.querySelectorAll(`[href='${a.getAttribute("href")}']`).forEach(el => {
const hl = el.parentElement.tagName === "LI" ? el.parentElement : el;
hl.classList.add("highlight");
console.log("Added to " + hl.tagName);
});
}
}
// Use event delegation to watch for mouseover and mouseout on the containers
const links = document.querySelector(".links");
links.addEventListener("mouseover", handler);
links.addEventListener("mouseout", removeHighlight);
const images = document.querySelector(".images");
images.addEventListener("mouseover", handler);
images.addEventListener("mouseout", removeHighlight);
.highlight {
border: 1px solid red;
}
<ul class="links">
<li><a href="https://...1">LINK 1</a></li>
<li><a href="https://...2">LINK 2</a></li>
<li><a href="https://...3">LINK 3</a></li>
</ul>
<div class="images">
<a href="https://...1">
<img src="1.png">
</a>
<a href="https://...2">
<img src="2.png">
</a>
<a href="https://...3">
<img class="ssoverview" src="3.png">
</a>
</div>