gpt4 book ai didi

javascript - 在菜单外单击时隐藏的下拉菜单

转载 作者:太空宇宙 更新时间:2023-11-04 13:34:18 24 4
gpt4 key购买 nike

我有一个简单的下拉脚本,我希望它在菜单外单击时隐藏所有打开的下拉菜单。但这似乎不起作用,有人知道为什么吗?

您可以在这里找到它:http://codepen.io/dr-potato/pen/rLleC?editors=101

HTML

<ul>
<li><a href="#">Home</a></li>
<li class="Navigation-listItem is-dropdown">
<a href="#">About</a>
<ul class="Navigation-list is-dropdown is-hidden">
<li>Johnny</li>
<li>Julie</li>
<li>Jamie</li>
</ul>
</li>
<li class="Navigation-listItem is-dropdown">
<a href="#">Contact</a>
<ul class="Navigation-list is-dropdown is-hidden">
<li>Johnny</li>
<li>Julie</li>
<li>Jamie</li>
</ul>
</li>
</ul>

CSS

.Navigation-list {
display: block;
}

.Navigation-list.is-hidden {
display: none;
}

JS

$(document).ready(function() {
$('.Navigation-listItem').click(function() {
$(this).children('.Navigation-list.is-dropdown').toggleClass('is-hidden');
});
});

/* Anything that gets to the document
will hide the dropdown */
$(document).click(function(){
$(".Navigation-listItem.is-dropdown").addClass('is-hidden');
});

/* Clicks within the dropdown won't make
it past the dropdown itself */
$(".Navigation-listItem.is-dropdown").click(function(e){
e.stopPropagation();
});

最佳答案

Working Fiddle

jQuery 代码

$(document).ready(function () {
$('.Navigation-listItem').click(function () {
$(this).children('.Navigation-list.is-dropdown').toggleClass('is-hidden');
});


/* Anything that gets to the document
will hide the dropdown */
$(document).on('click', function (event) {
if ($(event.target).closest('#menu').length == false) {
$(".Navigation-list.is-dropdown").addClass('is-hidden');
}
});

/* Clicks within the dropdown won't make
it past the dropdown itself */
$(".Navigation-listItem.is-dropdown ").click(function (e) {
e.stopPropagation();
});
});

this answer 的帮助下

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

24 4 0