Bootstrap 4中的“modal.modal不是函数”错误通常是由于以下几种原因之一引起的:
Bootstrap的模态框(Modal)是一个弹出窗口,可以用来显示重要内容或交互式组件。模态框通过JavaScript插件来控制其显示和隐藏。
以下是一个简单的模态框示例及其初始化方法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bootstrap Modal Example</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
$('#myModal').modal('show'); // 显示模态框
});
</script>
</body>
</html>
模态框广泛应用于需要临时展示额外信息的场景,如登录表单、警告提示、图片查看器等。
通过上述步骤和示例代码,通常可以解决“modal.modal不是函数”的问题。如果问题仍然存在,建议检查浏览器的控制台是否有其他错误信息,这可能会提供更多线索。
领取专属 10元无门槛券
手把手带您无忧上云