Bootstrap - 直接链接到模态窗口

6

能否拥有像这样的URL:

www.url.com/#imprint
(或类似的)以直接链接到打开了模态窗口的页面?

这是否可能?有什么提示吗?
谢谢!


是的,这是可能的。但这取决于您想要如何以及为何使用链接。 - Khamidulla
我最近几周只有一个预设页面,我需要从其他服务直接链接到我的版权声明。但是我的版权声明在预设页面的模态窗口中。这就是原因 ;) - Marek123
4个回答

6
你也许能够做这样的事情。
if (window.location.hash == "#imprint") {
     $('#myModal').modal('show');
}

4

为了方便未来的访问者/读者,我创建了一个非常简单的函数,可以动态地打开模态框,无需知道您要查找的确切ID。如果没有找到哈希值,它就会跳过。

/**
 * Function to open a bootstrap modal based on ID
 * @param int
 */
function directLinkModal(hash) {
  $(hash).modal('show');
}

/**
 * Call the function on window load
 * @param hash of the window
 */
directLinkModal(window.location.hash);

2
是的,这是可能的。只需检查URL即可:
function popModal() {
      // code to pop up modal dialog
    }

    var hash = window.location.hash;
    if (hash.substring(1) == 'modal1') {
      popModal();
    }

1
在浪费了几个小时后,我想到了这个解决方案。它可以根据在page-1中点击的链接,在page-2中打开不同的模态框。HTML代码基于Bootstrap官方模态框示例。 HTML | page-1.html
<body>    
  <a href="http://yourwebsi.te/page-2.html?showmodal=1">
  <a href="http://yourwebsi.te/page-2.html?showmodal=2">
</body>

JS | ShowModal.js

$(document).ready(function() {
  var url = window.location.href;
  if (url.indexOf('?showmodal=1') != -1) {
    $("#modal-1").modal('show');
  }
  if (url.indexOf('?showmodal=2') != -1) {
    $("#modal-2").modal('show');
  }
});

HTML | page-2.html

<body>    
  <div class="modal fade bd-example-modal-lg" id="modal-1" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
      <div class="modal-content">Your content</div>
    </div>
  </div>
  <div class="modal fade bd-example-modal-lg" id="modal-2" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
      <div class="modal-content">Your content</div>
    </div>
  </div>
  <script src="ShowModal.js"></script>
</body>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接