我试图打开和关闭一个模式,就像在互联网上提供的示例,但似乎我无法切换它(它总是打开),我甚至无法关闭它:
资本
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="container">
<button class="btn btn-primary" data-target="#campModal" data-toggle="modal"></button>
<div class="modal" id="campModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Create</h4>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<input type="text" name="Name" id="name"/>
</div>
<div class="form-group">
<input type="text" name="Password" id="password"/>
</div>
</form>
</div>
<div class="modal-footer">
<button class="btn btn-primary">Submit</button>
<button class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
一旦包含所需的依赖项,您的代码就可以运行。查看Google Chrome浏览器工具(或Firefox)上的控制台输出通常有助于找到缺少的依赖项。
.btn {
position: absolute;
/* Center the button */
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
<!-- JQuery CDN -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Popper.js CDN, required by Bootstrap -->
<script src="https://unpkg.com/popper.js"></script>
<!-- Bootstrap javascript CDN -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<!-- Bootstrap CSS CDN -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<body>
<div class="container">
<button class="btn btn-primary" data-target="#campModal" data-toggle="modal">OPEN MODAL</button>
<div class="modal" id="campModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Create</h4>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<input type="text" name="Name" id="name" />
</div>
<div class="form-group">
<input type="text" name="Password" id="password" />
</div>
</form>
</div>
<div class="modal-footer">
<button class="btn btn-primary">Submit</button>
<button class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
我用相同的代码创建了一个Codepen。