gpt4 book ai didi

javascript - 仅当在下拉菜单外单击鼠标时才关闭 Bootstrap 下拉菜单

转载 作者:可可西里 更新时间:2023-11-01 15:00:30 24 4
gpt4 key购买 nike

我有一个 Bootstrap 下拉菜单:

<div class="dropdown>
<a class="dropdown-toggle" href="#" id="menu1" data-toggle="dropdown"><img src="@imagePath" class="img-circle dropbtn" alt="Product" style="width:30px;height:30px;" /></a>

<ul class="dropdown-menu" id="productDD" role="menu" aria-labelledby="menu1"></ul>
</div>

现在 ul 通过 ajax 调用在页面加载时加载产品。问题是当我单击下拉列表中的任何产品时,下拉列表将关闭。但是我只想在 ul 之外的任何地方单击鼠标时关闭下拉列表

最佳答案

尝试这样的事情:

$(document).click(function(e) {
// get the clicked element
$clicked = $(e.currentTarget);
// if the clicked element is not a descendent of the dropdown
if ($clicked.closest('.dropdown').length === 0) {
// close the dropdown
$('.dropdown').removeClass('open');
}
});

关于javascript - 仅当在下拉菜单外单击鼠标时才关闭 Bootstrap 下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52166136/

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