Bootstrap Modal似乎不适用于Anchor Tag

时间:2018-11-07 20:50:50

标签: html css bootstrap-4 bootstrap-modal

我在我的html中对某些东西使用了模态。我尝试将其与锚标记一起使用,但未成功。我究竟做错了什么?这是我的代码:

    <div class="logo">MAIN</div>

    <ul>

        <li>
            <a href="#" class="navy-item">3</a>
            <div class="navy-content">
                <div class="navy-sub">
                    <ul>
                        <li>
                            <a data-toggle="modal" href="#myModal">3-1</a>
                        </li>
                        <li>
                            <a href="#">3-2</a>
                              </li>

    </ul>
    <span class="close-my">&times;</span>
    <span class="close-my">&boxH;</span>
    <span class="close-my">&minus;</span>

<div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">

          <!-- Modal content-->

          <div class="modal-content">

            <div class="modal-header">

              <button type="button" class="close" data-dismiss="modal">&times;</button>

              <h4 class="modal-title">Modal Header</h4>
            </div>

            <div class="modal-body">

              <p>Some text in the modal.</p>

            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
          </div>

        </div>
      </div>

我正在将Darkly Theme用作Bootstrap内容。其他事情正在工作,但不是这样。 您可以在这里找到主题:https://bootswatch.com/darkly/

1 个答案:

答案 0 :(得分:2)

您需要将idmyModal添加到您要定位的模式:

     <a data-toggle="modal" href="#myModal">ANCHOR</a>

                                  <!-- Modal -->

      <div id="myModal" class="modal">            <!-- add id here -->
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">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">
        <p>Modal body text goes here.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary">Save changes</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

更新

我最好的猜测是,bootstrapjQuery包含项之一可能存在问题。您的代码应按原样运行,请参见下面的示例:

<link href="https://bootswatch.com/4/darkly/bootstrap.min.css" rel="stylesheet">

<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<div class="logo">MAIN</div>

<ul>

  <li>
    <a href="#" class="navy-item">3</a>
    <div class="navy-content">
      <div class="navy-sub">
        <ul>
          <li>
            <a data-toggle="modal" href="#myModal">3-1</a>
          </li>
          <li>
            <a href="#">3-2</a>
          </li>

        </ul>
        <span class="close-my">&times;</span>
        <span class="close-my">&boxH;</span>
        <span class="close-my">&minus;</span>

        <div class="modal fade" id="myModal" role="dialog">
          <div class="modal-dialog">

            <!-- Modal content-->

            <div class="modal-content">

              <div class="modal-header">

                <button type="button" class="close" data-dismiss="modal">&times;</button>

                <h4 class="modal-title">Modal Header</h4>
              </div>

              <div class="modal-body">

                <p>Some text in the modal.</p>

              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              </div>
            </div>

          </div>
        </div>