jQuery菜单悬停问题
我有一个相当简单的菜单:
<ul id="menu">
<!-- TemplateBeginEditable name="mainmenu" -->
<li><a href="index.htm" title="" class="home">Home</a></li>
<li><a href="consumers.htm" title="" class="consumers">Consumers</a>
<ul id="consumer-menu">
<li> --snip--
并且:
$(document).ready(function() {
$('.consumers').hover( function() {
$("#consumer-menu").fadeIn();
}, function() {
$("#consumer-menu").fadeOut();
});
});
问题是,当您将鼠标从
我认为我需要的是一种选择两者的方法.consumers 及其所有子元素都在一个 jQuery 选择器语句中,可能类似于 $('.consumers > * (但包括 .consumers)。
感谢您的帮助!
约翰。
I have a fairly simple menu:
<ul id="menu">
<!-- TemplateBeginEditable name="mainmenu" -->
<li><a href="index.htm" title="" class="home">Home</a></li>
<li><a href="consumers.htm" title="" class="consumers">Consumers</a>
<ul id="consumer-menu">
<li> --snip--
and:
$(document).ready(function() {
$('.consumers').hover( function() {
$("#consumer-menu").fadeIn();
}, function() {
$("#consumer-menu").fadeOut();
});
});
The problem is that when you move your mouse away from <li class="consumers".. #consumer-menu dissapears (as it should), I've tried using $('.consumers, #consumer-menu also but that doesn't work (you mouse out from #consumer-menu to .consumers and the menu fades out then in again.)
What I think I need is a way to select both .consumers AND all it's children in one jQuery selector statement, perhaps something like $('.consumers > * (but including .consumers).
Thanks for helping!
John.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在给定上下文的情况下,我能想到的最简单的答案是像这样重构 HTML:
这应该在 jQuery 不变的情况下进行。
(使用 http://jsfiddle.net/MgbdN/ 进行测试)
The simplest answer I can think of given the context is to restructure your HTML like this:
which should work out with the jQuery unchanged.
(tested with http://jsfiddle.net/MgbdN/)
您对这里的解决方案有何看法: http://jsfiddle.net/WDktv/
基本上,每当您悬停时在 ul (或者更一般的菜单节点)上,那么您应该淡入其子菜单 ul (菜单节点)
What do you think of the solution here: http://jsfiddle.net/WDktv/
basically, whenever you hover over a ul (or a menu node, to be more general), then you should fade in its child menu ul (menu node)