【发布时间】:2018-02-16 15:49:30
【问题描述】:
我正在使用 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 但它不起作用,这意味着仍然单击菜单项引导下拉菜单正在关闭。
<script type="text/javascript">
$(function() {
$('.dropdown-menu button').on('click', function (event) {
console.log(event)
event.preventDefault();
})
});
</script>
【问题讨论】:
-
你试过
event.stopPropagation吗? api.jquery.com/event.stoppropagation 或event.stopImmediatePropagation? -
我试过
stopPropagation,但不是另一个。让我也试试。 -
@cale_b 还是同样的问题。问题是我也没有看到 console.log 输出..
标签: jquery html twitter-bootstrap bootstrap-4