Twitter bootstrap Bootstrap3.x:如何在单击模式触发器时更改url?
使用引导v3.3.5 我有一个使用urlTwitter bootstrap Bootstrap3.x:如何在单击模式触发器时更改url?,twitter-bootstrap,twitter-bootstrap-3,bootstrap-modal,Twitter Bootstrap,Twitter Bootstrap 3,Bootstrap Modal,使用引导v3.3.5 我有一个使用urldomain.com/companys 这是我的触发模式在该网页 <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
domain.com/companys
这是我的触发模式在该网页
<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">
但是,url不会更改为domain.com/companys#modal add company
urldomain.com/companys#modal add company
实际上也不会在重新加载时触发模式
我需要做什么才能具备以下条件:
domain.com/companys#modal add company
,并且domain.com/companys#modal add company
,则会显示模式在触发该模式的按钮的单击事件上使用此选项
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>
if(window.location.hash.substr(1)==“模态添加公司”){
$('模态添加公司').model('显示');
}
它应该可以工作。使用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>
$(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();
});
假设您希望escape按钮仍然能够关闭模式,并更改url,则整个代码块将如下所示:
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
$(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();
});
关于如何使用模式关闭事件,请参见。太棒了!坚持下去。整洁的解决方案,我喜欢:)