简体   繁体   English

如果另一个 div 有 class,则将 class 添加到一个 div

[英]Add class to a div if another div has a class

I did a lot of searching and read dozens of questions and answers on this topic and wrote the following code but it won't work for some reason.我做了很多搜索,阅读了很多关于这个主题的问题和答案,并编写了以下代码,但由于某种原因它不起作用。 I'm looking for help troubleshooting this.我正在寻求帮助解决此问题。

This is what I want to happen:这就是我想要发生的事情:

When the user hovers over a menu item, a dropdown appears.当用户将鼠标悬停在菜单项上时,会出现一个下拉菜单。

Then the entire header (currently has the ID #header) gets a new class (.header-new-class)然后整个 header(当前有 ID #header)得到一个新的 class(.header-new-class)

I found that when they hover over a menu item (li), the site automatically adds the class "open" to the menu item (the menu item already has the class.menu-item)我发现当他们在菜单项 (li) 上输入 hover 时,网站会自动将 class“打开”添加到菜单项(菜单项已经有 class.menu-item)

So my logic is, when the menu item has the class "open", it adds the class "header-new-class" to the div with the ID #header所以我的逻辑是,当菜单项有 class“打开”时,它将 class“header-new-class”添加到 ID 为#header 的 div

This is a very cleaned up version of the HTML:这是 HTML 的一个非常干净的版本:

<div ID="header">

    <div>
    <div>
    <div>
    <div>
    <div>
        <nav>
        <nav>
            <div>
            <div>
                <ul>
                    <li class="menu-item open">

                    </li>
                </ul>
            </div>
            </div>
        </nav>
        </nav>
    </div>
    </div>
    </div>
    </div>
    </div>

</div>

This is the code I wrote:这是我写的代码:

$(document).ready(function(jQuery) {
    if ($('.menu-item').hasClass('open')) {
        $('#header').addClass('header-new-class');
    }
});

It's not working.它不工作。 What am I doing wrong?我究竟做错了什么?

Thanks in advance.提前致谢。

if you want to add a class on the header when the mouse is on the menu item, do it like this, if you also want to remove the class then use the commented code below.如果你想在 header 上添加一个 class 当鼠标在菜单项上时,这样做,如果你还想删除 class 然后使用下面的注释代码。 if you have questions, feel free to ask如果您有任何疑问,请随时提问

 $(document).ready(function(){ $('.menu-item').on('mouseover',function(){ /*$('.menu-item').removeClass('open'); $(this).addClass("open");*/ if($(this).hasClass('open')){ $('#header').addClass('yourNewClass'); }else{ $('#header').removeClass('yourNewClass'); } }); /*$('.menu-item').on('mouseleave',function(){ $('.menu-item').removeClass('open'); $('#header').removeClass('yourNewClass'); });*/ });
 .yourNewClass.menu-item.open {color: red;}
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div ID="header"> <div> <div> <div> <div> <div> <nav> <nav> <div> <div> <ul> <li class="menu-item open"> item 1 </li> <li class="menu-item"> item 2 </li> <li class="menu-item"> item 3 </li> </ul> </div> </div> </nav> </nav> </div> </div> </div> </div> </div> </div>

Why you are set class for hover via jquery. CSS have functionality of :hover which give the same effect that you want.为什么要通过 jquery 将 class 设置为 hover。CSS 具有:hover的功能,这与您想要的效果相同。

 #header:hover{ background-color: lightBlue; }.menu-item:hover{ color: blue; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div ID="header"> <div> <div> <div> <div> <div> <nav> <nav> <div> <div> <ul> <li class="menu-item"> Sample Link 1 </li> <li class="menu-item"> Sample Link 2 </li> <li class="menu-item"> Sample Link 3 </li> </ul> </div> </div> </nav> </nav> </div> </div> </div> </div> </div> </div>

You can use same event many times.您可以多次使用同一事件。 So, this is achievable with normal .hover .所以,这是可以用普通.hover实现的。

   $(document).ready(function(){
   $('.menu-item').hover(function(){
      $('#header').addClass('header-new-class');
   },function(){
    /* function to remove class when hovering is over */
   })
    

If you absolutely need to check if the class open is present you can do it inside the hover function.如果你绝对需要检查 class open是否存在,你可以在 hover function 中进行。

You can also use mouseenter and mouseleave您还可以使用mouseentermouseleave

$(document).on({
    mouseenter: function () {
        //stuff to do on mouse enter
    },
    mouseleave: function () {
        //stuff to do on mouse leave
    }
}, ".selector");

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

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