我有一个 bootstrap(3.3.7) 页面,其中包含一个包含选项卡和链接的下拉菜单。此时,菜单在悬停时打开,选项卡起作用。
如果您在沙箱中删除此 JS 脚本:
$(document).ready(function(){
$(".dropdown").hover(
function() {
$('.dropdown-menu', this).stop( true, true ).slideDown("fast");
$(this).toggleClass('open');
},
function() {
$('.dropdown-menu', this).stop( true, true ).slideUp("fast");
$(this).toggleClass('open');
}
);
});
然后下拉菜单在单击时打开,但也会在下一次单击选项卡或其他菜单元素及其外部时关闭。
如果留下JS代码,菜单悬停时打开,点击选项卡(tabs)时不隐藏。
如何使菜单在单击时显示,同时防止菜单在选项卡和空下拉菜单上关闭?
显然,最简单的方法是将负责打开的代码从移动
hover()到click():同时,替换
toggleClass()为addClass()andremoveClass()