默认情况下,当html页面运行时,如何使模态可见

时间:2015-09-18 09:07:23

标签: javascript jquery html twitter-bootstrap css3

我正在创建一个默认显示在屏幕上的弹出式div(模态)。但是我成功地在按钮单击时打开它,但我希望每当我运行HTML页面时它都会打开。         这是我的代码。

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Bootstrap Example</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    </head>
    <body>

        <div class="container">
            <h2>Modal Example</h2>
            <!-- Trigger the modal with a button -->
            <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

            <!-- Modal -->
            <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>
        </div>
    </body>
</html>

我在使用按钮显示模态的地方。我使用data-target属性来调用模态。那么没有任何点击事件怎么做呢?

5 个答案:

答案 0 :(得分:4)

使用 .modal('show') 手动显示模型

&#13;
&#13;
$(document).ready(function() {
  $('#myModal').modal('show');
});
&#13;
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<div class="container">
  <h2>Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

  <!-- Modal -->
  <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>

</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

将班级in添加到<div class="modal fade" id="myModal" role="dialog">,以便

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

或者您可以在页面加载时调用以下函数

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

答案 2 :(得分:0)

document.ready()

打开模态

例如。

$(document).ready(function (){

    //open modal here
         $('#myModal').show();
      });

答案 3 :(得分:0)

对不起,我没有足够的声誉来添加评论。 根据{{​​3}}组件文档的介绍,无需将任何参数传递给.modal()方法。

$(document).ready(function() {
    $("#myModal").modal();
});

答案 4 :(得分:0)

将类添加到中,这样

<div class="modal fade show d-block" id="newModal" role="dialog">

或者您可以在页面加载时调用以下函数

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