Bootstrap模式在点击时不显示

时间:2017-10-09 17:48:17

标签: jquery html css twitter-bootstrap modal-dialog

当我尝试在Bootstrap中加载模态时,它会在我刷新页面时显示,但是当我点击按钮时它不会显示。我希望我的模式只有在用户点击按钮时才会弹出。

我的 html 文件设置如下:

<head>
    <meta charset="utf-8">
    <title>my title here</title>
    <link rel="stylesheet" type="text/css" href="/static/css/bootstrap.min.css">
    <script src="/static/js/jquery-3.2.1.min.js"></script>
    <script src="/static/js/popper.min.js"></script>
    <script src="/static/js/bootstrap.min.js"></script>
    <script src="/static/Highcharts-5.0.14/code/highcharts.js"></script>
    <script src="/static/Highcharts-5.0.14/code/modules/exporting.js"></script>
    <!-- Our Custom CSS -->
    <link rel="stylesheet" href="/static/css/style_charts_black.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>

我已从Bootstrap教程网站复制此代码:

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" style="float:right; margin-right:100px">
Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="myModalLabel">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>
      </div>
    </div>
  </div>
</div>

<script>$('#myModal').modal('show')</script> 

我在javascript控制台中没有得到任何jquery错误。我也尝试将上面的最后一行更改为 .modal(&#39; toggle&#39;),但它没有帮助。

3 个答案:

答案 0 :(得分:0)

您的代码很好,您需要将js文件包含在<body>标记中,而不是头部。

从他们的文件:

  

在关闭正文标记之前,在页面末尾添加我们的JavaScript插件,jQuery和Tether。

以下是我找到该信息的链接:

https://v4-alpha.getbootstrap.com/getting-started/introduction/

正文中包含的js文件的编解码器和您的代码正常工作:

https://codepen.io/egerrard/pen/MEGgJE

答案 1 :(得分:0)

您的代码工作正常,如果您只想在单击按钮时激活模态,则不需要脚本

  $(&#39;#myModal&#39;)模式。(&#39;秀&#39;)

&#13;
&#13;
<head>
    <meta charset="utf-8">
    <title>GSMA Intelligence &mdash; Visuals</title>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>

<!-- Button trigger modal -->
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </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>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

只需删除html标记中的数据切换即可 写

&#13;
&#13;
$('#myModal').modal('show')
or
$('#myModal').modal('toggle')
&#13;
&#13;
&#13;