gpt4 book ai didi

jquery - 单击时隐藏菜单

转载 作者:搜寻专家 更新时间:2023-10-31 22:33:07 25 4
gpt4 key购买 nike

我有这个下拉菜单,只要我的用户单击链接,它就会向下滑动。但是,每当用户单击页面上的某个位置(当然不在菜单上)时,我不知道如何使其向上滑动。

我的 CSS 看起来像这样:

<li class="da-header-button message">
<span class="da-button-count">32</span>
<a href="#">Notifications</a>
<ul class="subnav">
<li class="head"><span class="bold">Notificatons</span></li>
<li class="item"><a href="#">Under menu</a></li>
<li class="item"><a href="#">Under menu</a></li>
</ul>
</li>

我当前的 jQuery 代码如下所示:

jQuery("ul.topnav li").click(function() { //When trigger is clicked...  
//Following events are applied to the subnav itself (moving subnav up and down)
jQuery(this).find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click
jQuery(this).hover(function() {
}, function(){
jQuery(this).find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
});
});

如果我将最后一个 jQuery 函数编辑为:jQuery(this).hover(function() 到:jQuery(this).click(function()它不起作用,因为它只会向下滑动,然后立即向上滑动。

感谢您的帮助。

编辑:

谢谢大家!但是,如果我同时打开其中两个子菜单怎么办?我怎样才能避免这种情况?

我只想允许打开 1 个。

最佳答案

当单击传播到文档时向上滑动所有可见菜单:

$(document).on("click", function(){
$(".subnav:visible").slideUp();
});

然后通过停止事件传播来防止在菜单内时发生这种情况。

$("ul.topnav").on("click", "li", function(e){
e.stopPropagation();
$(".subnav:visible", $(this).siblings()).slideUp("fast");
$(".subnav", this).slideDown();
});

fiddle :http://jsfiddle.net/jonathansampson/qQsdN/

关于jquery - 单击时隐藏菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10901626/

25 4 0