简体   繁体   English

Javascript - 修改 hover 上 class 的所有实例的 css

[英]Javascript - modify css of all instances of class on hover

I'm trying to add listeners to all instances of class to modify css of ALL those instance when a user hovers over any one of those instances so far.我正在尝试将侦听器添加到 class 的所有实例,以在用户将鼠标悬停在迄今为止的任何一个实例上时修改所有这些实例的 css。 So I'm iterating over the 'slogan' node list adding the event listener to each of them but then the function to be executed also iterates over the node list to modify all of them but it works only for the first instance.因此,我正在迭代“口号”节点列表,将事件侦听器添加到每个节点,但随后要执行的 function 也会迭代节点列表以修改所有节点列表,但它仅适用于第一个实例。

That's what I've got这就是我所拥有的

const slogan = document.querySelectorAll(".slogan");

function sloganHoverOver(e) {
  slogan.forEach((s) => {
    s.styles.color = "red";
  });
}

slogan.forEach((s) => {
  s.addEventListener("mouseover", sloganHoverOver);
});

It changes the font color of only the one instance that is being hovered over.它仅更改被悬停的一个实例的字体颜色。 Also it throws the following error in the console:它还在控制台中引发以下错误:

TypeError: s.styles is undefined 

Use style not styles使用样式不是 styles

s.style.color = "red";

Or just use css :hover或者只使用 css :hover

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM