我有一个页面与4-5个不同的模态附加到它。每一个都有一个唯一的#值(obv)。我正在试图找到一种方法,从外部来源(在这种情况下是HTML电子邮件)链接到这些单独的模态。
例如:
电子邮件中的“单击此处查看方向”需要链接到-->> www.myurl.com/#directions
directions片段应该在页面加载时触发:函数,并让模式自动打开。
这是可能的吗?还是我只是在做梦?
发布于 2013-11-09 18:39:08
您可以使用hastag来决定要显示的内容。例如,
JS
$(document).ready(function () {
var target = document.location.hash.replace("#", "");
if (target.length) {
if(target=="option1"){
showModal("title1","content1");
}
else if(target=="option2"){
showModal("title2","content2");
}
}else{
showModal("no title","no content");
}
function showModal(title,content){
$('#myModal .modal-title').html(title);
$('#myModal .modal-body').html(content);
$('#myModal').modal('show');
}
});Bootstrap模式代码
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</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>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->这将显示option1的内容
http://jsfiddle.net/fFcS2/show/#option1
这将显示option2的内容
http://jsfiddle.net/fFcS2/show/#option2
代码
http://jsfiddle.net/fFcS2
发布于 2013-11-25 02:01:34
抱歉,我来晚了。您的解决方案100%有效。就像我已经有了多个模态一样,我像这样修改了代码:
<script type="text/javascript">
jQuery(document).ready(function($) {
var target = document.location.hash.replace("#", "");
if (target.length) {
if(target=="modal1link"){
$('#modal1').modal('show');
}
else if(target=="modal2link"){
$('#modal2').modal('show');
}
else if(target=="modal3link"){
$('#modal3').modal('show');
}
}else{
}
});
</script>发布于 2017-10-03 23:21:38
如果有人来这里寻找动态解决方案(在这里你不必预定义模式If ),你可以在这里找到:
$(document).ready(function() {
var url = window.location.href;
var modalToOpen = url.substring(url.indexOf("#"));
if(window.location.href.indexOf(modalToOpen) != -1) {
$(modalToOpen).modal("show");
}
});此脚本获取当前URL,查找URL中散列后的id,然后显示具有相应id的模态。
https://stackoverflow.com/questions/19874795
复制相似问题