简体   繁体   English

为了使每个 class 的效果单独工作(Javascript)

[英]To make the effect work separately for each class (Javascript)

There are three classes named "main-menu."有三个名为“主菜单”的类。 I want to apply the effect that underlines when I raise the mouse cursor in the area corresponding to each class.我想在每个 class 对应的区域中应用当我抬起鼠标 cursor 时下划线的效果。 I know that the current target is in the 0th index of the class and I have to use variables, but I don't know how to change that to suit the situation.我知道当前目标位于 class 的第 0 个索引中,我必须使用变量,但我不知道如何更改它以适应这种情况。 I would appreciate it if you could tell me how to solve this problem.如果您能告诉我如何解决这个问题,我将不胜感激。

在此处输入图像描述

window.onload = function(){
var target = document.getElementsByClassName("main-menu")[0];
target.addEventListener('mouseover', function(){
    target.style.borderBottom = "5px solid black";
});
target.addEventListener('mouseout', function(){
    target.style.borderBottom = "none";
});

} }

Try adding mouse hover css instead尝试添加鼠标 hover css 代替

.main-menu:hover {
    border-bottom: 5px solid black;
}

You can use loop and add effect to each element.您可以使用循环并为每个元素添加效果。 You can do this with css as well您也可以使用 css 执行此操作

.main-menu:hover { border-bottom: 5px solid black; }

 window.onload = function(){ var elements = document.getElementsByClassName("main-menu"); for(let index=0; index < elements.length;index++) { const target = elements[index]; target.addEventListener('mouseover', function(){ target.style.borderBottom = "5px solid black"; }); target.addEventListener('mouseout', function(){ target.style.borderBottom = "none"; }); }) }

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

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