我正在使用Bootstrap 4 alpha6。以下是我的下拉菜单HTML代码:
我想要的是,在某人点击
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" type="button" id="dropdownMenu2" aria-haspopup="true" aria-expanded="false">
Dropdown
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenu2">
<h6 class="dropdown-header">
<input class="form-control form-control-sm" type="text" placeholder="Search">
</h6>
<button class="dropdown-item" type="button">Action</button>
<button class="dropdown-item" type="button">Another action</button>
<button class="dropdown-item" type="button">Something else here</button>
</div>
</div>
我想要的是,在某人点击
.dropdown-menu
内的任何项目时,不要关闭下拉菜单。我编写了以下JS代码,但它并没有起作用,也就是说,当单击菜单项时,Bootstrap下拉菜单仍然会关闭。<script type="text/javascript">
$(function() {
$('.dropdown-menu button').on('click', function (event) {
console.log(event)
event.preventDefault();
})
});
</script>
event.stopPropagation
吗?https://api.jquery.com/event.stoppropagation/ 或者event.stopImmediatePropagation
? - random_user_namestopPropagation
但没有尝试另一个。让我也试试那个。 - hobby-coder