如何使模态在html页面运行时默认可见
How to make modal visible by default when html page runs
我正在创建一个弹出式div (modal),默认情况下将出现在屏幕上。然而,我成功地打开它的按钮点击,但我希望它默认情况下打开每当我运行HTML页面。这是我的代码。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Modal Example</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
我使用按钮显示模态。我使用数据目标属性来调用模态。那么如何在没有点击事件的情况下做到这一点呢?
使用 .modal('show')
手动显示模型
$(document).ready(function() {
$('#myModal').modal('show');
});
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<div class="container">
<h2>Modal Example</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
添加class in到html代码中,这样
<div class="modal fade show d-block" id="newModal" role="dialog">
或者您可以在页面加载
时调用以下函数$('#newModal').modal('show');
对不起,我没有足够的声誉来添加评论。根据bootstrap模态组件文档,不需要向.modal()方法传递任何参数。
$(document).ready(function() {
$("#myModal").modal();
});
将in
添加到<div class="modal fade" id="myModal" role="dialog">
中,使
<div class="modal fade in" id="myModal" role="dialog">
或者您可以在页面加载
时调用以下函数$('#myModal').modal('show');
open mode at document.ready()
。
$(document).ready(function (){
//open modal here
$('#myModal').show();
});
相关文章:
- 使用压缩的JavaScript文件(不是运行时压缩)
- 如何在运行时在angular 2中加载外部js脚本
- JavaScript错误:Microsoft JScript运行时错误:应为对象
- Google 脚本:用于创建日历活动的脚本运行时不会出错,但不会执行任何操作
- http.listen()在运行时接受终端命令
- 自定义运行时Can'在谷歌应用引擎中看不到我的自定义日志
- 实现比较方法的最佳实践是什么;s的比较类型是在运行时选择的
- JavaScript运行时是如何工作的
- 在运行时创建元素时移到一边时出错
- 如何在运行时在HTML5画布中绘制正方形
- 如何在运行时使用javascript隐藏图像
- 在运行时使用jquery准备表体会导致设计问题
- 使用主题运行时portlet liferay时出现Javascript错误
- JavaScript运行时事件循环现有技术
- Appcelerator Titanium:在运行时下载并解释JavaScript代码
- angularjs ng点击运行时标记不起作用
- 在函数运行时显示对话
- 如何在javascript中在运行时获取对象/数组
- Javascript运行时未包含在默认Gemfile中
- 如何使模态在html页面运行时默认可见