【问题标题】:Bootstrap modal close issue引导模式关闭问题
【发布时间】:2023-03-31 14:05:02
【问题描述】:

我正在尝试解决我的modal 中的错误。我正在使用Bootstrap、Angular 和ng-Route。我最近在mobile(然后在桌面)上注意到,当我在modal 打开的情况下按下后退按钮时,它会留下一个灰色的覆盖层,你不能click 任何东西。所以我发现部分解决问题的解决方案是添加这个脚本:

$(".modal").on("shown.bs.modal", function()  { // any time a modal is shown
      var urlReplace = "#/" + $(this).attr('id'); // make the hash the id of the modal shown
      history.pushState(null, null, urlReplace); // push state that hash into the url
    });

    // If a pushstate has previously happened and the back button is clicked, hide any modals.
    $(window).on('popstate', function() {
      $(".modal").modal('hide');
    });

这对用户 presses 和 back button 非常有效,但是当用户通过单击模式外部或点击转义关闭 modal 时,urlReplace 仍保留在 browser 地址栏中。 我想让它在modal 关闭时恢复到以前的状态。

如果该问题无法解决,我至少希望解决另一个问题,即:当用户通过单击外部或点击转义关闭modal 时,urlReplace 仍保留在browser 地址中很好,但是当用户然后转到click 我的nav bar 中的链接时,它不会将他们带到链接它转到一个空白页面,urlReplace 仍在地址栏中,然后我可以@987654341 @@nav bar 中的链接,它会转到正确的链接,我觉得很奇怪,不知道如何解决这个问题。

对此的任何想法或见解都会很棒!

【问题讨论】:

    标签: javascript angularjs twitter-bootstrap bootstrap-modal ngroute


    【解决方案1】:

    您可以添加一个处理程序来监视模式关闭事件,然后推送一个删除哈希的新历史记录:

    $(".modal").on("hidden.bs.modal", function()  { // any time a modal is hidden
        var urlReplace = window.location.toString().split('#', 1)[0]
        history.pushState(null, null, urlReplace); // push url without the hash as new history item
    });
    

    【讨论】:

    • 非常感谢!我认为发生了其他奇怪的事情,但只需要在关闭模式时输入正确的 url,我在导航中单击的链接现在可以正常工作!
    猜你喜欢
    • 1970-01-01
    • 2021-02-20
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-25
    • 1970-01-01
    相关资源
    最近更新 更多