触发 jQuery 的悬停()在锚点上显示包含 LI 内的下拉菜单?
我有一个水平导航栏,它是带有锚标记的无序列表。目前,它使用 jQuery 的悬停(鼠标悬停和鼠标移出)在每个 LI 中显示下拉导航。
我怎样才能做到这一点,而不是将鼠标悬停在 LI 上时触发下拉菜单,而将鼠标悬停在锚标记上时触发下拉菜单?当我将其更改为锚点时,当鼠标悬停在其顶部时,下拉列表总是消失,但是当我将 LI 设置为悬停()的目标时,它工作正常(我猜是因为所有下拉代码被包裹在 LI 中,因此光标仍然悬停在 LI 上)。
由于设计原因,我需要这样设置,因此没有任何方法可以更改它。我需要仅从锚点而不是 LI 触发悬停()。
I have a horizontal nav bar that is an unordered list with anchor tags. It currently uses jQuery's hover (mouseover and mouseout) to show drop-down navigation in each LI.
How can I make it so instead of triggering the drop-down when hoving over the LI it will be triggered when mousing over the anchor tag? When I changed it to the anchor the drop-down always disappears when mousing down over top of it, but it works fine when I set the LI to as the target for the hover() (I guess because all of the drop-down code is wrapped in the LI so the cursor is still hovering over the LI).
I need to set it up this way due to the design, so there isn't any way to change it. I need the hover() to be triggered just from the anchor and not the LI.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过锚点的悬停代码更改 LI 的类。当您离开 LI 时将其改回来。
You could change the class of the LI by the hover code for the anchor. Change it back when you move off the LI.
如果我理解正确的话,您遇到的问题是下拉列表在您将鼠标移至下拉菜单之前关闭。这可能是因为锚标记和下拉菜单之间存在间隙。
那么,您可以将打开方式更改为 a 标签,然后添加一个小计时器,即使您将鼠标移开,该计时器也可以使下拉列表保持打开状态一秒或多秒?无论如何,这样的添加是很好的,如果你不小心将鼠标移出了导航栏,并且因为它关闭而不得不返回,那么会很烦人。
编辑:另外,是的,JSFiddle 它。
The problem you're having, if I understand correctly, is that the dropdown closes before you get your mouse to it. This is probably because there's a gap between the anchor tag and the dropdown menu.
So how about, you change the opener to the a-tag, but then you add a little timer that keeps the dropdown list opened for one or more seconds even if you mouse out? Such an addition is nice to have later anyway, gets irritating if you accidentally mouse out of a navbar and have to go back because it closed.
Edit: Also, yes, JSFiddle it.
尝试在锚点中放置一个 span 标签:
Try putting a span tag inside your anchor: