Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Twitter bootstrap Bootstrap3.x:如何在单击模式触发器时更改url?_Twitter Bootstrap_Twitter Bootstrap 3_Bootstrap Modal - Fatal编程技术网

Twitter bootstrap Bootstrap3.x:如何在单击模式触发器时更改url?

Twitter 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

使用引导v3.3.5

我有一个使用url
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

url
domain.com/companys#modal add company
实际上也不会在重新加载时触发模式

我需要做什么才能具备以下条件:

  • 每当我触发模式并显示模式时,将url更改为
    domain.com/companys#modal add company
    ,并且
  • 如果我直接键入url
    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">&times;</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">&times;</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();
        });
    

    关于如何使用模式关闭事件,请参见。

    太棒了!坚持下去。整洁的解决方案,我喜欢:)