【问题标题】:Bootstrap 4 Modal Will Not Open on ClickBootstrap 4 模态不会在点击时打开
【发布时间】:2017-04-02 22:58:00
【问题描述】:

我无法打开我的模式。我只尝试了在 Bootstrap 4 站点上找到的纯 HTML,然后使用了一些 Javascript。我真的无法弄清楚我做错了什么。任何帮助将不胜感激!

enter code here

<div class="col-sm-3 col-centered  boxes" id="box1">

  <button type="button" class="btn btn-primary" id="box1modal" data-toggle="modal" data-target="#exampleModal">

    See More

  </button>

  <!-- Modal -->
  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">

    <div class="modal-dialog" role="document">

      <div class="modal-content">

        <div class="modal-header">

          <h5 class="modal-title" id="exampleModalLabel">Wedding Invites</h5>

          <button type="button" class="close" data-dismiss="modal" aria-label="Close">

            <span aria-hidden="true">&times;</span>

          </button>

        </div>

        <div class="modal-body">

          <img src="Wedding%20Invites.jpg">


        </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>

</div>
 `enter code here`<script type="text/javascript">

enter code here $("#box1modal").click(function(event){
enter code here$('#myModal').modal('show'); enter code here)};

【问题讨论】:

    标签: javascript jquery twitter-bootstrap bootstrap-modal bootstrap-4


    【解决方案1】:

    如果您使用 bootstrap,则无需额外的脚本。bootstrap 本身提供脚本来运行不同的动画和动作。我检查了你的代码。 错误在于行

       <button type="button" class="btn btn-primary" id="box1modal" data-toggle="modal" data-target="#exampleModal">
    

    看到您已将数据目标设置为#exampleModal,而您的模态ID 为“myModal”。只需将上面的代码更改为

       <button type="button" class="btn btn-primary" id="box1modal" data-toggle="modal" data-target="#myModal">
    

    而且它可以在没有任何其他脚本的情况下正常工作。 希望这会有所帮助...

    【讨论】:

    • 非常感谢。那行得通!我不敢相信我花了这么多时间试图弄清楚这一点!
    【解决方案2】:

    您已将数据目标指定为 examplemodal,并将 div 的 id 指定为 mymodal。两者应该是一样的,这就是问题所在。

    【讨论】:

      【解决方案3】:

      我测试你的代码,一切都很好,只是你必须确保

      1) 添加引用 2)你的代码必须在 $(document).ready(function(){ ---- }

      <html>
      <head> 
      
      <!-- Latest compiled and minified CSS -->
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <!-- jQuery library -->
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
      
      <!-- Latest compiled JavaScript -->
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      </head>
      
      <body> 
      <div class="col-sm-3 col-centered  boxes" id="box1">
      
        <button type="button" class="btn btn-primary" id="box1modal" data-toggle="modal" data-target="#exampleModal">
      
          See More
      
        </button>
      
        <!-- Modal -->
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      
          <div class="modal-dialog" role="document">
      
            <div class="modal-content">
      
              <div class="modal-header">
      
                <h5 class="modal-title" id="exampleModalLabel">Wedding Invites</h5>
      
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
      
                  <span aria-hidden="true">&times;</span>
      
                </button>
      
              </div>
      
              <div class="modal-body">
      
                <img src="Wedding%20Invites.jpg">
      
      
              </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>
      
      </div>
      
      
      </body>
      </hmtl>
      
      <script>
      
      
      $(document).ready(function(){
      
      $('#box1modal').click(function(){
      alert("btn click")
      $('#myModal').modal('show')
      });
      
      });
      
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-21
        • 2017-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多