jQuery mouseover 的问题,以获得更好看的 css 菜单

发布于 2024-08-02 18:55:17 字数 2678 浏览 4 评论 0原文

我的网站上有一个水平菜单,使用 drupal 中的好菜单。当您将鼠标悬停在 li 元素上时,将为显示的链接设置背景图像。我遇到的问题是,当您将鼠标悬停在父菜单项的子项上时,该图像会消失。正在消失的图像是使用以下 css 样式设置的:

  ul.nice-menu li a:hover, ul.nice-menu li.active a {
    background: url(images/bg-li-active.png) no-repeat scroll left bottom;
  }

  ul.nice-menu li * a:hover, ul.nice-menu li.active * a {
    background: none repeat;
    color:#000000;
  }

我已经设置了 jsbin 来测试这一点: http://jsbin.com /ogegu

看起来它按预期工作。菜单展开,并且在子 ul 上时似乎只有父元素发生变化。但是,当我使用实际尝试设置背景图像的代码时,png 不会出现。

菜单如下所示(查看 http://jsbin.com/ogegu 上的 jsbin 演示):

<div class="content">  
  <ul class="nice-menu nice-menu-down" id="nice-menu-1"><li id="menu-240" class="menu-path-front"><a href="/" title="" class="active">Home</a></li>  
    <li id="menu-660" class="menu-path-node-20"><a href="/content/about-us" title="About Us">About Us</a></li>  
    <li id="menu-238" class="menuparent menu-path-node-3"> 
      <a href="/services" title="">Services</a> 
      <ul> 
        <li id="menu-988" class="menu-path-node-29"><a href="/content/business-advisory" title="Business Advisory">Business Advisory</a></li>  
        <li id="menu-244" class="menu-path-node-10"><a href="/content/network-design" title="Network Design">Network Design</a></li>  
      </ul>  
    </li>  
    <li id="menu-239" class="menu-path-node-2"><a href="/content/clients" title="">Clients</a></li>  
    <li id="menu-327" class="menu-path-partners"><a href="/partners" title="">Partners</a></li>  
    <li id="menu-631" class="menu-path-principals"><a href="/principals">Principals</a></li>  
  </ul>  
</div>  

这里是我尝试使用的实际 JavaScript:

jQuery(document).ready(function(){
  jQuery('#nice-menu-1 li ul').hide();
  jQuery('#nice-menu-1 li.menuparent').hover(function() {
      jQuery(this).children('ul').show('fast');
      jQuery(this).children('a:eq(0)').css({ "background": "url(images/bg-li-active.png) no-repeat scroll left bottom"});
      return false;
  },
  function() {
      jQuery(this).children('ul').hide('fast');
      jQuery(this).children('a:eq(0)').css({"background": "none"});
      return false;
  });
});

我对使用 jQuery 相当陌生,任何人都可以提供关于为什么这没有达到我预期的建议吗?理想情况下,我希望将鼠标悬停或鼠标悬停在 menuparent 或 menuparent 的子级上来设置第一个子级标签的背景。更好的解决方案是只使用 css 而不使用 JavaScript。

I have a horizontal menu on my site using nice-menus in drupal. When you hover over an li element, a background image is set for the link that displays. The problem I'm having is that this image disappears when you hover over a parent menu item's children. The image that is disappearing is set using this css style:

  ul.nice-menu li a:hover, ul.nice-menu li.active a {
    background: url(images/bg-li-active.png) no-repeat scroll left bottom;
  }

  ul.nice-menu li * a:hover, ul.nice-menu li.active * a {
    background: none repeat;
    color:#000000;
  }

I have set up jsbin to test this: http://jsbin.com/ogegu

It looks like it works as expected. The menu expands, and only the parent element seems to be altered when over the child ul. However when I use code that actually attempts to set the background image, the png does not appear.

the menu looks like this (check out the jsbin demo at http://jsbin.com/ogegu ):

<div class="content">  
  <ul class="nice-menu nice-menu-down" id="nice-menu-1"><li id="menu-240" class="menu-path-front"><a href="/" title="" class="active">Home</a></li>  
    <li id="menu-660" class="menu-path-node-20"><a href="/content/about-us" title="About Us">About Us</a></li>  
    <li id="menu-238" class="menuparent menu-path-node-3"> 
      <a href="/services" title="">Services</a> 
      <ul> 
        <li id="menu-988" class="menu-path-node-29"><a href="/content/business-advisory" title="Business Advisory">Business Advisory</a></li>  
        <li id="menu-244" class="menu-path-node-10"><a href="/content/network-design" title="Network Design">Network Design</a></li>  
      </ul>  
    </li>  
    <li id="menu-239" class="menu-path-node-2"><a href="/content/clients" title="">Clients</a></li>  
    <li id="menu-327" class="menu-path-partners"><a href="/partners" title="">Partners</a></li>  
    <li id="menu-631" class="menu-path-principals"><a href="/principals">Principals</a></li>  
  </ul>  
</div>  

Here is the actual JavaScript that I am trying to use:

jQuery(document).ready(function(){
  jQuery('#nice-menu-1 li ul').hide();
  jQuery('#nice-menu-1 li.menuparent').hover(function() {
      jQuery(this).children('ul').show('fast');
      jQuery(this).children('a:eq(0)').css({ "background": "url(images/bg-li-active.png) no-repeat scroll left bottom"});
      return false;
  },
  function() {
      jQuery(this).children('ul').hide('fast');
      jQuery(this).children('a:eq(0)').css({"background": "none"});
      return false;
  });
});

I am fairly new to using jQuery, can anyone offer a suggestion as to why this isn't doing what I'd expect? Ideally, I would like any hover or mouseover on the menuparent or menuparent's children to set the background of the first a tag that is a child. Even better, would be a solution that uses only css and no JavaScript.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尤怨 2024-08-09 18:55:17

您需要将子级包裹在父级内,然后仅将其悬停在父级上。
由于子项位于父项内部,因此当您将子项悬停在父项上时,您仍然将其悬停在父项上。
看这个例子
http://www.bunchacode.com/programming/shadow-menu/

you need to wrap the children inside the parent and then .hover on the parent only.
since children is inside the parent, so when you hover the children, you are still hovering the parent.
see this example
http://www.bunchacode.com/programming/shadow-menu/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文