从导航栏打开“模态”窗口

时间:2014-05-15 18:18:40

标签: html5 twitter-bootstrap

我正试图从导航栏打开一个模态窗口。我已经按照了一些很好的教程,一旦我按下我的链接一个模态打开,问题是模型窗口看起来不能因为某些原因而编辑,甚至无法关闭模态窗口中的X.同样在导航栏外工作得很好。

我想知道为什么会这样。

我的代码如下:

<ul class="nav navbar-nav pull-right">
    <li class="active"><a href="#">Home</a></li>
    <li><a href="#" data-toggle="modal" data-target="#basicModal">Contact Us</a></li>
</ul>

<div class="modal fade" id="basicModal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Basic Modal</h4>
            </div>
            <div class="modal-body">
                <h3>Modal Body</h3>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">
                    Save changes
                </button>
            </div>
        </div>
    </div>
</div>


屏幕截图:

enter image description here

4 个答案:

答案 0 :(得分:9)

这包括in the official documentation

  

模态标记放置

     

始终尝试将模式的HTML代码放在文档的顶层位置,以避免影响模式外观和/或功能的其他组件。

<div>类的modal放在导航栏之外。您可以将模态触发<a>留在导航栏中。

答案 1 :(得分:2)

<nav class="navbar bg-secondary">
        <div
            class="collapse navbar-collapse float-right" id="navcol-1" style="color:rgb(255,255,255);">
            <ul class="nav navbar-nav ml-auto" role="navigation">
                <li><a href="#myModal" data-toggle = "modal" data-target= "#myModal" class="nav-link">Contact us</a></li>
                </ul>
        </div>
    </div>
</nav>
<div class="modal fade" id="myModal">
    <div class="modal-dialog">
        <div class="modal-content"> 
            <div class="modal-header">
                <h5 class="modal-title">For your Queries</h5> 
            </div>
            <div class="modal-body">
                IF you have any questions, Mess Manager Office number is <strong>+01234567890</strong> or you can email us by <strong>Ouremail@domain.com</strong>>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss = "modal">Close</button>
            </div>
        </div>
    </div>
</div>

答案 2 :(得分:0)

我遇到了同样的问题。我可以修复它的唯一方法是在导航栏中的链接上添加jQuery单击以覆盖引导程序默认值。像这样:

$("#navbar_register_btn").on("click",function(e){
    e.preventDefault();
    $('#basicModal').modal('show');
})

您需要在导航栏中为您的链接指定一个&#34; navbar_register_btn&#34;在这种情况下。

答案 3 :(得分:0)

  <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalCenterTitle">Modal title</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          ...
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary">Save changes</button>
        </div>
      </div>
    </div>
  </div>