这里先放一下头部的所有引用:
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
(可以参考官方文档https://getbootstrap.net/docs/components/dropdowns/#split-button-dropdowns或者菜鸟教程https://www.runoob.com/bootstrap4/bootstrap4-dropdowns.html)
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 实例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="dropdown">
<a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown link
</a>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</div>
</div>
</body>
</html>
效果如下:
主要改写了以下几点:
//设置鼠标悬停展开下拉菜单
function dropdownOpen(){
$('.dropdown').mouseover(function(){
$(this).addClass('show');
//查找当前元素子节点中的对象,修改其class,这样
//这样当有多个下拉菜单时,可以分别独立处理,而不会出现都下拉的bug
$(this).find(".dropdown-menu").addClass('show');
}).mouseout(function(){
$(this).find(".dropdown-menu").removeClass('show');
$(this).removeClass('show');
});
}
<div class="dropdown-menu" style="min-width: unset">
<a class="dropdown-item" style="padding: 4px 10px 4px 10px;" href="#">专委会简介</a>
<a class="dropdown-item " style="padding: 4px 10px 4px 10px;" href="#">组织结构</a>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>dropdown</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script>
$(function () {
dropdownOpen();
});
function dropdownOpen(){
$('.dropdown').mouseover(function(){
$(this).addClass('show');
//查找当前元素子节点中的对象,修改其class,这样
//这样当有多个下拉菜单时,可以分别独立处理,而不会出现都下拉的bug
$(this).find(".dropdown-menu").addClass('show');
}).mouseout(function(){
$(this).find(".dropdown-menu").removeClass('show');
$(this).removeClass('show');
});
}
</script>
</head>
<body>
<div class="container-fluid">
<div class="row" style="min-height: 20px"></div>
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-1">
<div class="dropdown" >
<!-- 去掉dropdown-toggle类,可以将按钮右侧的下三角图标去除-->
<button type="button" class="btn dropdownBtn btn-secondary" data-toggle="dropdown">
专委会介绍
</button>
<!-- 减小menu的宽度可以重置min-width来做到-->
<div class="dropdown-menu" style="min-width: unset">
<!-- 通过设置padding来控制每个子标签的大小和文字位置-->
<a class="dropdown-item" style="padding: 4px 10px 4px 10px;" href="#">专委会简介</a>
<a class="dropdown-item " style="padding: 4px 10px 4px 10px;" href="#">组织结构</a>
<a class="dropdown-item " style="padding: 4px 10px 4px 10px;" href="#">工作条例</a>
<a class="dropdown-item " style="padding: 4px 10px 4px 10px;" href="#">领导成员</a>
<a class="dropdown-item " style="padding: 4px 10px 4px 10px;" href="#">文件资料</a>
</div>
</div>
</div>
</div>
</div>
</body>
</html>