使用 Bootstrap v3.3.5
我有一个使用网址domain.com/companies
这是我对该网页模式的触发器。
<a href="#modal-add-company" class="btn btn-effect-ripple btn-effect-ripple btn-success" data-toggle="modal"><i class="fa fa-plus"></i> New Company</a>
这将成功触发此模态
<div id="modal-add-company" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
但是,网址不会更改为domain.com/companies#modal-add-company
网址domain.com/companies#modal-add-company
实际上也不会在重新加载时触发模式。
我需要做什么才能拥有以下内容:
domain.com/companies#modal-add-company
,domain.com/companies#modal-add-company
,则会显示模式答案 0 :(得分:15)
使用jQuery这可能是一个可行的解决方案
$(document).ready(function(){
$(window.location.hash).modal('show');
$('a[data-toggle="modal"]').click(function(){
window.location.hash = $(this).attr('href');
});
});
假设您有一个触发器来关闭模态,如下所示:
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
您可以在上面的代码块下面添加:
$('button[data-dismiss="modal"]').click(function(){
var original = window.location.href.substr(0, window.location.href.indexOf('#'))
history.replaceState({}, document.title, original);
});
假设您希望转义按钮仍然能够关闭模态,并且还更改了网址,整个代码块将如下所示:
$(window.location.hash).modal('show');
$('a[data-toggle="modal"]').click(function(){
window.location.hash = $(this).attr('href');
});
function revertToOriginalURL() {
var original = window.location.href.substr(0, window.location.href.indexOf('#'))
history.replaceState({}, document.title, original);
}
$('.modal').on('hidden.bs.modal', function () {
revertToOriginalURL();
});
感谢https://stackoverflow.com/a/13201843/80353关于如何使用模态关闭事件。
答案 1 :(得分:1)
在触发模态
的按钮的 onclick事件上使用此功能onclick="window.location.hash = 'modal-add-company';"
在bootstrap.js
之后编写以下脚本代码<script>
if(window.location.hash.substr(1) == 'modal-add-company'){
$('#modal-add-company').modal('show');
}
</script>
它应该有用。