【问题标题】:Change text color of a span with Javascript while hovering on seperated div将鼠标悬停在单独的 div 上时使用 Javascript 更改跨度的文本颜色
【发布时间】:2021-03-07 13:31:44
【问题描述】:

我有一个菜单点,在它下面有一个单独的 div / mega 菜单。我触发了通过 Javascript 显示的巨型菜单。当我将鼠标悬停在大型菜单上时,菜单中所需的跨度应该用另一种颜色突出显示,并且背景颜色也应该改变。您可以在代码中看到我如何尝试解决它(包括 cmets)。你能帮我么。我不知道为什么我不能通过 .getElementsByClassName 触发它!?

//Showing mega menu on hover over menu point

document.getElementById("menu-item-136").addEventListener("mouseover", mouseOver);
document.getElementById("menu-item-136").addEventListener("mouseout", mouseOut);

function mouseOver() {
  document.getElementById("mega-menu").style.display = "block";
}

function mouseOut() {
  document.getElementById("mega-menu").style.display = "none";
}



//Let mega menu stay visible when hovering over it
//Style menupoint when hovering over mega menu div (NOT WORKING)!

document.getElementById("mega-menu").addEventListener("mouseover", mouseOver);
document.getElementById("mega-menu").addEventListener("mouseout", mouseOut);

function mouseOver() {
  document.getElementById("mega-menu").style.display = "block";
  document.getElementsByClassName (".aux-menu-label").style.color = "yellow";
}

function mouseOut() {
  document.getElementById("mega-menu").style.display = "none";
  document.getElementsByClassName (".aux-menu-label").style.color = "";
}
.menu-item-136 {
background-color: grey;
height: 50px;
}

.menu-item-136:hover {
background-color:green;
}

.aux-menu-label {
color:blue;
}

.mega-menu-1 {
display: none;
background-color: green;
height: 200px;
}
<div>
<li id="menu-item-136" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-136 aux-menu-depth-0 aux-menu-root-2 aux-menu-item">
            <a href="#" class="aux-item-content">
                <span class="aux-menu-label"><i aria-hidden="true" class="services auxicon-list"></i>&nbsp;&nbsp;Leistungen</span>
            <span class="aux-submenu-indicator"></span></a>
</div>



<div id="mega-menu" class="mega-menu-1">content</div>

感谢您的帮助!

【问题讨论】:

  • 您的示例使用的是 getElementsByClassName,它指的是具有同一类的多个元素。但是,您的 HTML 仅显示一个类为 aux-menu-label 的元素。你会在你的项目中有更多这样的东西吗? - 此外,您有一个 li,它只是在没有父 ulol 的情况下漂浮。你应该把它清理一下。
  • 您好,感谢您的回复。那么,这仅在我进入多个班级时才有效吗?我用鼠标悬停和“ElementsByClass”做了很多尝试,但没有任何效果。我希望这仅适用于这一个菜单点(Leistungen)。但是菜单中有更多的“辅助菜单标签”元素……尽管它们的父级具有唯一的 li id。在我看来,这似乎很容易。 “将鼠标悬停在该 div 上时更改此跨度的颜色”​​。嗯
  • 在下面查看我的答案,您的代码还有其他问题,但我只是为您的getElementsByClassName 问题提供了答案,并在您的 HTML 中添加了其他标签以使代码正常工作。如果您包含更多的 HTML,那会有所帮助。

标签: javascript colors highlight megamenu


【解决方案1】:

你的代码有点乱,但是你错误地调用了你的类:

这个:

document.getElementsByClassName (".aux-menu-label")

应该是这样的:

document.getElementsByClassName ("aux-menu-label")

此外,当使用getElementsByClassName 时,您将获得一个类似数组的对象,其中包含具有您指定的类的所有元素。

考虑到这一点,您必须运行一个循环来定位具有您想要应用的样式的元素。

以下代码是我们如何定位多个标签并在悬停时将它们更改为黄色:

  var labels = document.getElementsByClassName("aux-menu-label");
  for (var i = 0; i < labels.length; i++) {
    labels[i].style.color = "yellow"
  }

当您运行下面的 sn-p 时,您会看到我使用类似的代码将颜色恢复为蓝色 onmouseout

详细了解getElementsByClassNamehere

//Including this to show you how to target CSS child elements as described in your comment
var childElement = document.querySelector('#menu-item-136 .aux-item-content'); 
childElement.style.backgroundColor = "white";
console.log(childElement);

//Showing mega menu on hover over menu point

document.getElementById("menu-item-136").addEventListener("mouseover", mouseOver);
document.getElementById("menu-item-136").addEventListener("mouseout", mouseOut);

function mouseOver() {
  document.getElementById("mega-menu").style.display = "block";
}

function mouseOut() {
  document.getElementById("mega-menu").style.display = "none";
}



//Let mega menu stay visible when hovering over it
//Style menupoint when hovering over mega menu div (NOT WORKING)!

document.getElementById("mega-menu").addEventListener("mouseover", mouseOver);
document.getElementById("mega-menu").addEventListener("mouseout", mouseOut);

function mouseOver() {
  document.getElementById("mega-menu").style.display = "block";
  var labels = document.getElementsByClassName("aux-menu-label");
  for (var i = 0; i < labels.length; i++) {
    labels[0].style.color = "yellow"
  }
}

function mouseOut() {
  document.getElementById("mega-menu").style.display = "none";
  var labels = document.getElementsByClassName("aux-menu-label");
  for (var i = 0; i < labels.length; i++) {
    labels[i].style.color = "blue"
  }
}
.menu-item-136 {
  background-color: grey;
  height: 50px;
}

.menu-item-136:hover {
  background-color: green;
}

.aux-menu-label {
  color: blue;
}

.mega-menu-1 {
  display: none;
  background-color: green;
  height: 200px;
}
<div>
  <li id="menu-item-136" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-136 aux-menu-depth-0 aux-menu-root-2 aux-menu-item">
    <a href="#" class="aux-item-content">
      <span class="aux-menu-label"><i aria-hidden="true" class="services auxicon-list"></i>&nbsp;&nbsp;Leistungen</span>
      <span class="aux-submenu-indicator"></span></a>
</div>



<div id="mega-menu" class="mega-menu-1">content</div>

<div>
  <li id="menu-item-136" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-136 aux-menu-depth-0 aux-menu-root-2 aux-menu-item">
    <a href="#" class="aux-item-content">
      <span class="aux-menu-label"><i aria-hidden="true" class="services auxicon-list"></i>&nbsp;&nbsp;Leistungen</span>
      <span class="aux-submenu-indicator"></span></a>
</div>



<div id="mega-menu" class="mega-menu-1">content</div>

编辑:我已包含以下 javascript 来向您展示如何定位子元素并将 CSS 应用于它们。下面的代码将针对#menu-item-136 的孩子并将其背景颜色更改为白色。运行sn -p查看。

var childElement = document.querySelector('#menu-item-136 .aux-item-content'); 
childElement.style.backgroundColor = "white";
console.log(childElement);

【讨论】:

  • 哇,谢谢!那肯定让我更接近。真的很酷。现在唯一的问题是,所有标签都变成黄色。我能以某种方式避免这种情况吗?将颜色添加到标签“Leistungen”会很好。是否可以使用 #menu-item-123 &gt; .aux-item-content 指定 CSS 中的选择?那太棒了!
  • 如果您想定位特定标签,例如第一个标签,您可以使用labels[0].style.color = "yellow",因为 getElementsByClassName 的输出是一个数组,您可以通过它们的索引定位特定元素。索引从 0 开始,所以labels[0].style.color = "yellow" 将定位第一个链接,labels[1].style.color = "yellow" 将定位第二个链接,依此类推...运行更新后的 sn-p。
  • 要回答您的问题,您可以使用 javascript 定位子元素,如下所示:var childElement = document.querySelector('#menu-item-136 .aux-item-content'); console.log(childElement); 试一试。
  • 还更新了我的答案,向您展示了上述代码的示例。
  • 你杀了它!非常感谢你。从 Javascript 开始让我想起了我从 HTML 或 CSS 开始时的第一步。我以为我会死。但是像你这样的人认为会变得更清楚。我仍然需要学习很多东西。感谢你! :)
【解决方案2】:

在您的代码中 如果我们向 megamenu 包装器添加一些边距,这将无法正常工作 当指针从菜单项中移出时,菜单关闭。

我已经解决了这个问题

它看起来像 WordPress 菜单。请查看下面的示例,这将有助于使用多个超级菜单您需要将 megamenu 数据 id 与菜单类项映射。

codepen example

 [].forEach.call(document.querySelectorAll('nav > ul > li'), function (link) {
        link.addEventListener('mouseover', coloringHandler);
        link.addEventListener('mouseout', decoloringHandler);
    });

    [].forEach.call(document.querySelectorAll('.megamenu'), function (menu) {
        menu.addEventListener('mouseover', megamenuHover );
    });


    var state = false;
    
    function coloringHandler(){
        state = false;
        hideAllMegamenu();
        // add class to current hover element
        this.classList.add('active');
        var Classes = this.classList; // getting all class list
        Classes.forEach(name => {
            var megaMenu  = document.querySelectorAll('[data-id="'+name+'"]'); // check if have any mached elements with class name 
            if(megaMenu.length == 1 ){
                megaMenu[0].classList.add('active');
                state = true;

                megaMenu[0].addEventListener('mouseover', megamenuHover );
                megaMenu[0].addEventListener('mouseout', megamenuHoverOut );

                return;
            }
        });
    }

    function decoloringHandler(){
        if( state == false ){
            this.classList.remove('active');
            hideAllMegamenu();
        }
    }

    function hideAllMegamenu(){
        // change elemets as you want
        [].forEach.call(document.querySelectorAll('nav > ul > li'), function (li) {
            li.classList.remove("active");
        });
        // .megamenu is common class
        [].forEach.call(document.querySelectorAll('.megamenu'), function (menues) {
            menues.classList.remove('active');
        })
    }

    function  megamenuHover() {
        this.classList.add('in-hover');
    }

    function megamenuHoverOut() {
        hideAllMegamenu();
    }
nav ul{
    display:flex;
    list-style:none;
    }

    li{
    margin:0px 10px;
    }

    a{
    background:green;
    display:block;
    color:white;
    padding:10px 20px;
    }

    ul li.active a{
    background:red;
    }


    .megamenu{
    background: red;
    height:200px;
    pointer-events: none;
    opacity:0;
      position:absolute;
            width:100%;
      padding:20px;
      color:#fff;
      transition:all .5s ease;
      transform:translateY(50px);
    }

    .megamenu.active{
    opacity:1;
    pointer-events: all;
      transform:translateY(0px);
    }
<h1>Hover over the menu Items</h1> 
<nav>
   <ul>
     <li class="menu-item-136">
       <a href="#"><span>Home</span></a>
     </li>
     <li class="menu-item-137">
       <a href="#"><span>Contact us</span></a>
     </li>
     <li class="menu-item-138">
       <a href="#"><span>Danushka</span></a>
     </li>
     
     <li class="menu-item-139">
       <a href="#"><span>About us</span></a>
     </li>
   </ul>
</nav>

<div class="megamenu" data-id="menu-item-137">
  Lorem ipsum dolor sit amet consectetur adipisicing elit.
</div>
<div class="megamenu" data-id="menu-item-138">
  Danushka Megamenu... Add data id for mapping
</div>

【讨论】:

  • 哇,你用 Javascript 构建了一个完整的大型菜单,完全杀死了它! :D 谢谢你真是太好了! ;) 这将有助于未来的项目。但也许您可以帮我指定类名(请参阅我在“Aib Codes Daily”回复下的评论)。它现在可以工作,但我需要只针对 id“menu-item-136”下的特定类。我能做到吗?感谢您在这里的工作! :)
  • 是的,你可以,根据需要使用 megame 数据 id,当悬停菜单项时会出现“menu-item-136”类 :) &lt;div class="megamenu" data-id="menu-item-136"&gt; Lorem ipsum dolor sit amet consectetur adipisicing elit. &lt;/div&gt; 您可以将字段添加到 wordpress 后端和将其用作 megamenu 数据 ID
  • 如果为 megamenu 元素添加一些边距,您当前的解决方案将不起作用。如果您正在使用它,请不要在 megame 菜单 div 中添加任何边距顶部值:p
  • 因为我想直接在导航点的“链接”下的巨型菜单,我这样做的方式是可以的。因此,当我将鼠标悬停在边距上时,div 会消失。感谢您的信息! ;)
猜你喜欢
  • 1970-01-01
  • 2014-07-14
  • 2019-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-31
  • 2012-08-21
相关资源
最近更新 更多