gpt4 book ai didi

javascript - onclick外部菜单使用jquery隐藏子菜单

转载 作者:行者123 更新时间:2023-11-28 10:53:25 25 4
gpt4 key购买 nike

这是我的代码:

$("#menu li:not(:first-child)").click(function () {

$(this).siblings().removeClass('active');
$menu = $(this).find('ul');
$othermenu = $(this).siblings().find('ul');
if($othermenu.is(':visible') == true) {
$othermenu.animate({opacity:'0', easing:'easeOutQuad'}, 500).css({display:'none'});
}
if($menu.is(':visible') == false) {
$(this).addClass('active');
$menu.css({display:'block'}).animate({opacity:'1', easing:'easeOutQuad'}, 500);

} else {
$(this).removeClass('active');

$menu.animate({opacity:'0', easing:'easeOutQuad'}, 500).css({display:'none'});

}
});

使用此代码在单击菜单时显示/隐藏子菜单。我想改变,就好像我当时点击了外部菜单一样,我也想隐藏子菜单。我的 Html 代码是这样的

<div id="menu" class="showmenubox submenucheck">
<ul> <li><a href="#" >Home</a> </li>
<li><a href="javascript:void(0);">About </a>
<ul ><li>
<a href="#"><strong>aaaa</strong></a>
<a href="#">bbb</a>
</li>
</ul></li>
<li><a href="javascript:void(0);">Contact</a>
<ul><li>
<a href="#"><strong>aaaa</strong></a><a href="#">bbb</a>
</li>
</ul>
</li> </ul> </div>

最佳答案

您想测试单击文档时单击了什么...

$(document).click(function (e) {
var $container = $("#menu");
if (!$container.is(e.target) // if the target of the click isn't the container...
&& $container.has(e.target).length === 0) // nor a descendant of the container
{

$container.find('.active ul').animate({opacity:'0', easing:'easeOutQuad'}, 500).css({display:'none'});

}
});

http://jsfiddle.net/kW2Nb/

关于javascript - onclick外部菜单使用jquery隐藏子菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22708852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com