jquery remove add classes on hover

六眼飞鱼酱① 提交于 2019-12-11 06:25:16

问题


Alright so i found a code and it seems to be working but I need to add something to that code to prevent it from firing if I hover over its child menu/sub-menu element but still needs to fire when i hover over other items or other items childern menu's this is the current script

$(document).ready(function() {     
   $("#navbar li:hover, #navbar li:sfhover").hover(function(){     
       $('#navbar .current-menu-parent, #navbar li.current-menu-item').addClass("non-ahover");    
   },     
   function(){    
       $('#navbar li.current-menu-parent, #navbar li.current-menu-item').removeClass("non-ahover");     
    });
}); 

So my current active menu would have either the .current-menu-parent or .current-menu-item. my sub menu starts with a < ul > class inside of the parent's < li > class and the sub-menus class is < ul class="sub-menu" > then proceeds with < li > classes for its items.

So my problem is when i hover over my sub-menu that is active by one of its items being the current page or by the parent page being viewed the script above fires and causes ugliness is there a snippet of code i can put inside of this script that stop the call if the current sub-menu is visible so it does not apply the class the script wants to apply?

I know might be a little confusing but if you need clarification at all please let me know and i will help as best as i can.


回答1:


  1. Bind an event handler to the children
  2. Have that handler call stopPropagation()

http://api.jquery.com/event.stopPropagation/



来源:https://stackoverflow.com/questions/5836765/jquery-remove-add-classes-on-hover

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!