我有一个推特引导下拉菜单。所有Twitter Bootstrap用户都知道,下拉菜单在点击时关闭(甚至在里面点击)。
为了避免这种情况,我可以很容易地在下拉菜单上附加一个单击事件处理程序,并简单地添加著名的event. stoppropagation()。
<ul class="nav navbar-nav">
<li class="dropdown mega-dropdown">
<a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-list-alt"></i> Menu item 1
<span class="fa fa-chevron-down pull-right"></span>
</a>
<ul class="dropdown-menu mega-dropdown-menu">
<li>
<div id="carousel" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-slide-to="0" data-target="#carousel"></li>
<li class="active" data-slide-to="1" data-target="#carousel"></li>
</ol>
<div class="carousel-inner">
<div class="item">
<img alt="" class="img-rounded" src="img1.jpg">
</div>
<div class="item active">
<img alt="" class="img-rounded" src="img2.jpg">
</div>
</div>
<a data-slide="prev" role="button" href="#carousel"
class="left carousel-control">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a data-slide="next" role="button" href="#carousel"
class="right carousel-control">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
</li>
</ul>
</li>
</ul>
这看起来很简单,也是一种非常常见的行为,然而,由于carousel控件(以及carousel指示器)事件处理程序被委托给文档对象,这些元素上的单击事件(prev/next控件,…)将被“忽略”。
$('ul.dropdown-menu.mega-dropdown-menu').on('click', function(event){
// The event won't be propagated up to the document NODE and
// therefore delegated events won't be fired
event.stopPropagation();
});
依赖于Twitter Bootstrap下拉隐藏/隐藏事件不是一个解决方案,原因如下:
为两个事件处理程序提供的事件对象没有提供对所单击元素的引用
我无法控制下拉菜单内容,因此添加标志类或属性是不可能的
这个小提琴是正常的行为,这个小提琴是添加了event.stopPropagation()。
更新
谢谢罗曼的回答。我也找到了一个答案,你可以在下面找到。
[Bootstrap 4 Alpha 6][Rails]
对于rails开发人员来说,e.s stoppropagation()将导致link_to的data-method不等于get的不良行为,因为它默认将所有请求返回为get。
为了解决这个问题,我建议这个解决方案,它是通用的
$('.dropdown .dropdown-menu').on('click.bs.dropdown', function() {
return $('.dropdown').one('hide.bs.dropdown', function() {
return false;
});
});
$('.dropdown .dropdown-menu').on('click.bs.dropdown', function() {
return $('.dropdown').one('hide.bs.dropdown', function() {
return false;
});
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<ul class="nav navbar-nav">
<li class="dropdown mega-dropdown">
<a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-list-alt"></i> Menu item 1
<span class="fa fa-chevron-down pull-right"></span>
</a>
<ul class="dropdown-menu mega-dropdown-menu">
<li>
<div id="carousel" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-slide-to="0" data-target="#carousel"></li>
<li class="active" data-slide-to="1" data-target="#carousel"></li>
</ol>
<div class="carousel-inner">
<div class="item">
<img alt="" class="img-rounded" src="img1.jpg">
</div>
<div class="item active">
<img alt="" class="img-rounded" src="img2.jpg">
</div>
</div>
<a data-slide="prev" role="button" href="#carousel" class="left carousel-control">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a data-slide="next" role="button" href="#carousel" class="right carousel-control">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
</li>
</ul>
</li>
</ul>
jQuery:
<script>
$(document).on('click.bs.dropdown.data-api', '.dropdown.keep-inside-clicks-open', function (e) {
e.stopPropagation();
});
</script>
HTML:
<div class="dropdown keep-inside-clicks-open">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">
Dropdown Example
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a href="#">HTML</a></li>
<li><a href="#">CSS</a></li>
<li><a href="#">JavaScript</a></li>
</ul>
</div>
演示:
泛型:
https://jsfiddle.net/kerryjohnson/omefq68b/1/
这个解决方案的演示:http://jsfiddle.net/kerryjohnson/80oLdtbf/101/
如果使用return false或stopPropagation()方法,您可能会遇到一些问题,因为您的事件将被中断。
试试这段代码,它的工作很好:
$(function() {
$('.dropdown').on("click", function (e) {
$('.keep-open').removeClass("show");
});
$('.dropdown-toggle').on("click", function () {
$('.keep-open').addClass("show");
});
$( ".closeDropdown" ).click(function() {
$('.dropdown').closeDropdown();
});
});
jQuery.fn.extend({
closeDropdown: function() {
this.addClass('show')
.removeClass("keep-open")
.click()
.addClass("keep-open");
}
});
在HTML中:
<div class="dropdown keep-open" id="search-menu" >
<button class="btn dropdown-toggle btn btn-primary" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fa fa-filter fa-fw"></i>
</button>
<div class="dropdown-menu">
<button class="dropdown-item" id="opt1" type="button">Option 1</button>
<button class="dropdown-item" id="opt2" type="button">Option 2</button>
<button type="button" class="btn btn-primary closeDropdown">Close</button>
</div>
</div>
如果你想关闭dropdrown:
`$('#search-menu').closeDropdown();`