我有一个 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/