jQuery悬停()和悬停状态应保持
我是 jQuery 的新手,我尝试这样的操作:
<ul id="CatNavi">
<li class="CatLevel1 SubMenue">Top Level 1
<div class="sub">
<ul>
<li class="CatLevel2">Sub Level 1</li>
<li class="CatLevel2">Sub Level 1</li>
</ul>
</div>
</li>
</ul>
jquery 看起来像这样:
$(function() {
$("li.CatLevel1 a").hover(function() {
$(this).next("div.sub").slideDown(500);
},function() {
$(this).next("div.sub").slideUp(200);
});
});
它工作正常。当我将鼠标悬停在顶层 1 时,会显示 div class="sub",但我无法单击此 div 中的元素,因为悬停状态消失了。
我做错了什么?
格瑞兹 罗恩
I'm new to jQuery and I try something like this:
<ul id="CatNavi">
<li class="CatLevel1 SubMenue">Top Level 1
<div class="sub">
<ul>
<li class="CatLevel2">Sub Level 1</li>
<li class="CatLevel2">Sub Level 1</li>
</ul>
</div>
</li>
</ul>
And the jquery looks like this:
$(function() {
$("li.CatLevel1 a").hover(function() {
$(this).next("div.sub").slideDown(500);
},function() {
$(this).next("div.sub").slideUp(200);
});
});
It works fine. When I hover the Top Level 1 the div class="sub" is shown but I can't click the elements in this div because the hover state disappears.
What am I doing wrong?
Greetz
Ron
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的函数应如下所示:
当您离开
时,
mouseleave
事件会触发...这就是.hover()
处理程序是隐藏同级的。mouseleave
不会在进入子级时触发,因此将悬停事件放在上意味着它们在进入子级时不会触发您输入子
/
元素。
Your function should look like this:
When you move off the
<a>
themouseleave
event fires...which is what the second handler in that.hover()
handler is, hiding the siblings.mouseleave
doesn't fire when entering a child, so having the hover events on the<li>
instead means they won't fire when you enter the child<div>
/<ul>
elements.将悬停更改为在
而非
链接上工作。这意味着当您单击子菜单中的链接时,光标仍位于
我假设上面的 HTML 是错误的,您的意思是悬停在“Top Level 1”上,这应该是在 jQuery 中启动悬停功能的链接。
Change the hover to work on the
<li>
not<a>
link. This means the cursor is still on top of the<li>
while you click on the link in the submenu. That way the second hover function only fires when you completely move off the list element, which the submenu is inside.I'm assuming the above HTML is wrong and you mean hovering over the 'Top Level 1', which should be a link for the hover function to kick in in the jQuery.