保持悬停状态 (jQuery)
HTML:
<ul class="dropdown">
<li><a href="#">Item 1</a></li>
<li>
<a href="#">Item 2</a>
<div class="submenu">something</div>
</li>
</ul>
jQuery:
$j("ul.dropdown li").hover(function () {
$j(this).addClass("hover");
$j('div.submenu', this).css('visibility', 'visible');
}, function () {
$j(this).removeClass("hover");
$j('div.submenu', this).css('visibility', 'hidden');
});
... 菜单工作正常,但是当显示 div.submenu
并将光标移动到它时,打开此子菜单的链接会丢失其“悬停”类,怎么办当链接和子菜单打开时,我在它们上保持悬停状态吗?
我尝试过这个,但它不起作用:
$j("ul.dropdown li").hover(function () {
$j(this).addClass("hover");
$j('div.submenu', this).css('visibility', 'visible').hover(function () {
$j(this).prev('a').addClass('hover');
}, function () {
$j(this).prev('a').removeClass('hover');
});
}, function () {
$j(this).removeClass("hover");
$j('div.submenu', this).css('visibility', 'hidden');
});
HTML:
<ul class="dropdown">
<li><a href="#">Item 1</a></li>
<li>
<a href="#">Item 2</a>
<div class="submenu">something</div>
</li>
</ul>
jQuery:
$j("ul.dropdown li").hover(function () {
$j(this).addClass("hover");
$j('div.submenu', this).css('visibility', 'visible');
}, function () {
$j(this).removeClass("hover");
$j('div.submenu', this).css('visibility', 'hidden');
});
... the menu works fine, but when the div.submenu
is shown and I move the cursor to it, the link that opened this submenu loses its 'hover' class, how do I maintain hover state on both the link and the submenu when they're open?
I tried this, but its not working:
$j("ul.dropdown li").hover(function () {
$j(this).addClass("hover");
$j('div.submenu', this).css('visibility', 'visible').hover(function () {
$j(this).prev('a').addClass('hover');
}, function () {
$j(this).prev('a').removeClass('hover');
});
}, function () {
$j(this).removeClass("hover");
$j('div.submenu', this).css('visibility', 'hidden');
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据我的经验,js 菜单带来的麻烦远大于其价值。如果您不想要任何花哨的淡入淡出或滚动效果,有一个完全可行的 CSS 解决方案。
但!无论如何,这可能与 CSS 有关。我认为人们想太多了。您真正想要的只是让您的链接看起来仍然悬停在上方——您的显示效果很好。好的。您可以使用 CSS 和 jQuery 来做到这一点:
现在,如果您已经有了那种样式(
.hover
很重要),那么您的子菜单也应该在悬停时消失,因此还有其他事情正在发生。In my experience, js menus are more trouble than they're worth. There's a perfectly workable CSS solution if you don't want any fancy fadein or rolldown effects.
But! This might have to do with CSS anyway. I think people are overthinking it. All you really want is for your link to still look hovered over--your display's fine. Okay. You can do this with CSS and jQuery:
Now, if you have that style (
.hover
is important) there already, your submenu should be disappearing on hover, too, so something else is afoot.您可能需要考虑子菜单 div 也是 jQuery 元素的一部分,同时附加悬停效果。可能是这样的:
You may need to consider the submenu div also part of jQuery elements, while attaching hover effect to. May be sth like this:
您无法在未悬停的元素上维持悬停状态:) 如果可能,请尝试将
div.submenu
放入ul.dropdown li
中。另一个解决方案是停止依赖hover()
并使用另一种操作类的方法。不要使用hover()
来切换类,而是使用一些函数来执行此操作并在需要时调用它。You cannot maintain the hover state on an element that you don't hover :) Try putting the
div.submenu
inside theul.dropdown li
, if it is possible. Another solution is to stop relying onhover()
and use another way of manipulating your classes. Don't use thehover()
to toggle classes, use some function to do it and call it when you need it.