当鼠标悬停在子级上时,保留:对父级的悬停效果
我的菜单出现了新问题。
我想当鼠标悬停在子级上时保持父级悬停效果处于活动状态。
我知道我也许可以使用 jQuery?但如果可能的话,我真的想将它保留在 CSS 中。
并且不必为所有元素提供唯一的 id ...
I have a new problem with my menu.
I want to keep the parent hover effect active when mouse is over child.
I know I could maybe use jQuery? but I really wanna keep it in CSS if possible.
And without having to give all elements unique id's...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 :hover 选择器放在
li
元素上,而不是锚点http://jsfiddle.net/ 293mV/
Put the :hover selector on the
li
element rather than the anchorhttp://jsfiddle.net/293mV/
如果你想使用仅 CSS 的方式,我强烈推荐你,你将不得不稍微改变你的标记。
当您检查
a
的父级(li
)上的:hover
时,a
仍将位于>:hover
当您进入子菜单时。基本上我们正在这样做:
我们将
a
扩展到li
的维度。因此,li
的行为似乎与a
类似。唯一的区别是,li
是a
和子菜单ul
的父菜单。这里是 JSFiddle。
HTML 保持不变:
CSS 发生变化:
If you want to use a CSS only way, what I highly recommend to you, you would have to change your markup a little bit.
When you check for
:hover
ona
's parent (theli
)a
will still be on:hover
when you go down the submenu.Basically we're doing this:
We will expand the
a
to the dimensions of theli
. So theli
seems to behave like thea
. The only difference is, that theli
is the parent of thea
and the submenuul
.JSFiddle here.
HTML stays the same:
CSS changes:
您无法使用 CSS 影响父项;你需要使用JavaScript。如果您不喜欢 ID,您可以使用 jQuery 的相对选择器
.parent()
、.next()
等。You cannot effect parent items with CSS; you need to use JavaScript. If you don't like IDs you can use jQuery's relative selectors
.parent()
,.next()
etc.