当用户点击弹出模式的标签时,我正在尝试实现一项功能。
问题
这是我的代码(只是来自bootstrap网站的代码)
HTML
<label id="some-label">Something</label>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
JS
$('#some-label').on('click', function() {
//alert("hello");
$('#myModal').show();
});
答案 0 :(得分:4)
您的代码无法正常工作,因为您绕过标准引导程序逻辑来显示模态。您正在调用show()
,但由于opacity: 0
类,该元素仍设置为fade
,因此不会显示任何内容。
要解决此问题,请在触发元素(data
和data-target
)上使用相应的data-toggle
属性。然后你不需要包含任何明确的JS,因为Bootstrap将为你做繁重的工作:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<label id="some-label" data-toggle="modal" data-target="#myModal">Something</label>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
&#13;