Modal在我的代码中不起作用

时间:2016-01-05 05:10:33

标签: javascript html twitter-bootstrap

我尝试了一切并反复检查了一遍。但我仍然无法找到我的代码有什么问题。

我认为img和模态的id是对的。头部也没有问题......

你能看看我的代码吗?我真的很感激。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Just a Portfolio</title>
    <link rel="stylesheet" href="css/bootstrap.css">
    <link href='https://fonts.googleapis.com/css?family=Luckiest+Guy' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="css/style.css">
    <script type="text/javascript" src="js/bootstrap.js"></script>
    <script type="text/javascript" src="js/jquery-2.1.4.min.js">
    </script>

  </head>

  <body>
      <div class="container">

        <div class="row">
          <div class="col-md-6">
            <img src="img/1.png" alt="heiheihei">
          </div>
          <div class="col-md-6 text-right">
            <h1 class="text-uppercase text-thin title-super">Weikang Zhang</h1>
            <h3>It's Now or Never.</h3>
          </div>
        </div>

        <div class="row">
          <div class="col-md-12">
            <img class="title-logo img-responsive" src="img/2.jpg" alt="stussy guy">
          </div>
        </div>

        <div class="row">
          <div class="col-md-12">
            <h2 class="text-muted">Hello</h2>
          </div>
        </div>

        <div class="row">
          <div class="col-md-4">
            <img class="img-responsive" src="img/a.jpg" data-toggle="modal" data-target="#p1">
          </div>
          <div class="col-md-4">
            <img class="img-responsive" src="img/b.jpg">
          </div>
          <div class="col-md-4">
            <img class="img-responsive" src="img/c.jpg">
          </div>
        </div>

        <div class="row">
          <div class="col-md-4">
            <h3 class="text-center">Man</h3>
          </div>
          <div class="col-md-4">
            <h3 class="text-center">Book</h3>
          </div>
          <div class="col-md-4">
            <h3 class="text-center">Woman</h3>
          </div>
        </div>
      </div>

<!-- Modal -->
<div class="modal fade" id="p1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title" id="myModalLabel">Favorite App Page</h4>
      </div>
      <div class="modal-body">
        <img class="img-responsive" src="images/a.jpeg">
        This was my first project in this class. I learned a lot about HTML and CSS.
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

  </body>
</html>

3 个答案:

答案 0 :(得分:2)

问题在这里;

<script type="text/javascript" src="js/bootstrap.js"></script>
<script type="text/javascript" src="js/jquery-2.1.4.min.js">

bootstrap依赖于jquery,因此在bootstrap.js之前加载jquery

应该是

<script type="text/javascript" src="js/jquery-2.1.4.min.js">
<script type="text/javascript" src="js/bootstrap.js">

答案 1 :(得分:1)

jquery js之前加入bootstrap js。然后试试。

答案 2 :(得分:1)

除了在jquery之前将来源置于bootstrap.js之外,您还需要显示模态。这可以通过$('#modalId').modal('show')完成,只需将其放入JS:

即可
$(document).ready(function(){

  $('#p1').modal('show');

});

这是Working Demo