【问题标题】:Launch Bootstrap Modal on Page Load在页面加载时启动引导模式
【发布时间】:2012-05-01 06:58:33
【问题描述】:

我根本不懂 JavaScript。 Bootstrap 文档说

通过 JavaScript 调用模态框:$('#myModal').modal(options)

我不知道如何在页面加载时调用它。使用 Bootstrap 页面上提供的代码,我可以在元素单击时成功调用 Modal,但我希望它在页面加载时立即加载。

【问题讨论】:

  • @rlemon - 不一定;问题完全是关于 Bootstrap 模式的,而不是一般在页面加载时运行的功能。 Bootstrap modal 也可以通过 CSS 类进行操作,请参阅其中一个答案。
  • 这会有所帮助。更简单:w3schools.com/jsref/… 带有 onload 事件

标签: javascript html twitter-bootstrap events bootstrap-modal


【解决方案1】:

只需将您要在页面加载时调用的模式包装在文档头部的 jQuery load 事件中,它应该会弹出,如下所示:

JS

<script type="text/javascript">
    $(window).on('load', function() {
        $('#myModal').modal('show');
    });
</script>

HTML

<div class="modal hide fade" id="myModal">
    <div class="modal-header">
        <a class="close" data-dismiss="modal">×</a>
        <h3>Modal header</h3>
    </div>
    <div class="modal-body">
        <p>One fine body…</p>
    </div>
    <div class="modal-footer">
        <a href="#" class="btn">Close</a>
        <a href="#" class="btn btn-primary">Save changes</a>
    </div>
</div>

您仍然可以通过如下链接调用页面中的模式:

<a class="btn" data-toggle="modal" href="#myModal">Launch Modal</a>

【讨论】:

  • 这个答案是正确的。需要注意的一件事是,这在文档就绪事件中根本不起作用:$(document).ready( ... 。它仅适用于窗口加载事件:$(window).load( ... .
  • 另一个重要提示:很多人将他们的 javascript 包含在正文的末尾。在这种情况下,onload 函数必须包含在最后,在包含之后。
  • @racl101 这对我来说正好相反(你的帖子已经有 2 年了,所以事情可能已经改变了)。它仅适用于$(document).ready( ...,当我将此脚本存储在我的 Modal 的 MVC PartialView 中时,当用户单击某些内容时会动态添加,所以也许这就是原因。具有讽刺意味的是,您对 做什么的警告让我弄清楚如何让它为我工作。那谢谢啦? :)
  • 隐藏对我不起作用(作为类),因为引导程序推翻了它,因此不会显示任何内容。它看起来很糟糕(不像模态),当你按下按钮时它也没有关闭。 javascript 部分有效,但我建议使用 GAURAV MAHALE 在他的回答中使用的模态的 html 部分,但请注意删除他的模态类中的“显示”一词。
  • 先包含jQuery库!
【解决方案2】:

您不需要javascript 来显示模态

最简单的方法是用“in”代替“hide”

class="modal fade hide"

所以

class="modal fade in"

您需要在按钮关闭时添加onclick = "$('.modal').hide()"

PS:我认为最好的方法是添加 jQuery 脚本:

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

【讨论】:

  • 我已经尝试过这种方法,但遇到了两个问题。 1)模态不会关闭。 2)模态背景消失了。有什么想法吗?
  • 上面的 jquery 解决方案效果很好。一行代码就可以解决问题。 @NickGreen 试试看:class="modal fade"。删除“隐藏”和“在”。这在我的情况下有效,但尚未上线,否则我会发布链接。
【解决方案3】:

只需添加以下内容-

class="modal show"

工作示例-

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<div class="container">
  <!-- 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 show" 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>

【讨论】:

  • 此解决方案不起作用:对话框永久卡在页面顶部。此外,如果您将“淡入淡出”类与“模态”一起使用,则根本不会出现对话框。
  • 这取决于模态需要显示的场景,在我的情况下,我希望模态被永久卡住。
  • 也遇到了Bob的问题
  • 如果关闭按钮什么都不做,为什么还要设置它们?有没有办法让它工作?
  • @boris,将此添加到模态关闭按钮:onclick = "$('.modal').removeClass('show').addClass('fade');"
【解决方案4】:

你可以试试这个可运行的代码-

$(window).load(function()
{
    $('#myModal').modal('show');
});
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<div class="container">
  <!-- 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>

更多信息请关注here

认为你有完整的答案。

【讨论】:

  • 感谢发布头部所需的链接:)
  • 对我来说,这段代码是唯一一个运行得很漂亮的代码。谢谢。
【解决方案5】:

2021 年更新

引导程序 5

现在 Bootstrap 不再需要 jQuery,使用 JavaScript 显示模式很容易..

var myModal = new bootstrap.Modal(document.getElementById('myModal'), {})
myModal.toggle()

Demo

引导程序 4

Bootstrap 4 的模态标记略有变化。以下是如何在页面加载时打开模态,并可选择延迟显示模态...

$(window).on('load',function(){
    var delayMs = 1500; // delay in milliseconds
    
    setTimeout(function(){
        $('#myModal').modal('show');
    }, delayMs);
});    

<div class="modal fade" id="myModal">
      <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">My Modal</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">
                    ...
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary mx-auto" data-dismiss="modal">Close</button>
                </div>
            </div>
      </div>
</div>

Demo

【讨论】:

【解决方案6】:

关于 Andre 的 Ilich 所说的你必须添加的内容 js脚本 在你称之为 jquery 的行之后:

<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.1.js" type="text/javascript"></script>

<script type="text/javascript">
     $(window).load(function(){
         $('#myModal').modal('show');
      });
</script>

在我的情况下这是问题希望它有所帮助

【讨论】:

    【解决方案7】:

    使用 Bootstrap 3 和 jQuery(2.2 和 2.3)测试

    $(window).on('load',function(){
      $('#myModal').modal('show');
    });
    
    
    
    <!-- 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"><i class="fa fa-exclamation-circle"></i>&nbsp; //Your modal Title</h4>
            </div>
            <div class="modal-body">
              //Your modal Content
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
            </div>
          </div>
    
        </div>
    </div>
    

    https://jsfiddle.net/d7utnsbm/

    【讨论】:

    • @EduardoCuomo 是的,它可以工作的jsfiddle.net/bsmpLvz6 这个例子是使用 Bootstrap 3.3 和 jQuery 2.2 制作的
    【解决方案8】:

    在我的情况下,添加 jQuery 来显示模式不起作用:

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

    这似乎是因为模态有属性 aria-hidden="true":

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

    需要删除该属性以及上面的 jQuery:

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    

    请注意,我尝试将模式类从 modal fade 更改为 modal fade in,但没有成功。此外,将类从 modal fade 更改为 modal show 会阻止模式关闭。

    【讨论】:

      【解决方案9】:

      这是一个解决方案 [无需 javascript 初始化!]

      我找不到示例没有使用 javascript $('#myModal').modal('show') 初始化您的模式,所以这里有一个关于如何在页面上没有 javascript 延迟 的情况下实现它的建议加载。

      1. 使用类和样式编辑模态容器 div:

      &lt;div class="modal in" id="MyModal" tabindex="-1" role="dialog" style="display: block; padding-right: 17px;"&gt;

      1. 用类和风格编辑你的身体:

        &lt;body class="modal-open" style="padding-right:17px;"&gt;

      2. 添加模态背景 div

        &lt;div class="modal-backdrop in"&gt;&lt;/div&gt;

      3. 添加脚本

        $(document).ready(function() {
            $('body').css('padding-right', '0px');
            $('body').removeClass('modal-open');
            $('.modal-backdrop').remove();
        
            $('#MyModal').modal('show'); });
        

        将会发生的情况是,您的模式的 html 将在页面加载时加载,而无需任何 javascript(无延迟)。此时您无法关闭模态,这就是为什么我们有 document.ready 脚本,以便在加载所有内容时正确加载模态。我们实际上将删除自定义代码,然后(再次)使用 .modal('show') 初始化模式。

      【讨论】:

      • 好建议。谢谢!
      【解决方案10】:

      您无需编写任何 JavaScript,只需通过数据属性即可激活模式。

      选项“show”设置为true,在初始化时显示模态:

      <div class="modal fade" tabindex="-1" role="dialog" data-show="true"></div>
      

      【讨论】:

        【解决方案11】:

        就像其他人提到的那样,使用 display:block 创建你的模态

        <div class="modal in" tabindex="-1" role="dialog" style="display:block">
        ...
        

        将背景放在页面上的任何位置,不一定要位于页面底部

        <div class="modal-backdrop fade show"></div> 
        

        然后,为了能够再次关闭对话框,添加这个

        <script>
            $("button[data-dismiss=modal]").click(function () {
                $(".modal.in").removeClass("in").addClass("fade").hide();
                $(".modal-backdrop").remove();
            });
        </script>
        

        希望对你有帮助

        【讨论】:

          【解决方案12】:

          除了 user2545728 和 Reft 答案之外,没有 javascript 但带有 modal-backdrop in

          要添加的 3 件事

          1. 在 .modal 类之前包含 modal-backdrop in 类的 div
          2. style="display:block;" 到 .modal 类
          3. fade in 与 .modal 类一起

          示例

          <div class="modal-backdrop in"></div>
          
          <div class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="channelModal" style="display:block;">
          
              <div class="modal-dialog modal-lg" role="document">
          
                  <div class="modal-content">
          
                      <div class="modal-header">
          
                          <h4 class="modal-title" id="channelModal">Welcome!</h4>
          
                      </div>
          
                      <div class="modal-body" style="height:350px;">
          
                          How did you find us?
          
                      </div>
          
                  </div>
          
              </div>
          
          </div>
          

          【讨论】:

            【解决方案13】:
            <div id="ModalStart" class="modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-body">
                      <p><i class="icon-spinner icon-spin icon-4x"></i></p>
                </div>
            </div>
            

            即使没有 Javascript,您也可以在开始时显示它。只需删除“隐藏”类即可。

            class="Modal"
            

            【讨论】:

              【解决方案14】:

              在 bootstrap 3 中,您只需要通过 js 初始化模态,如果在页面加载的那一刻,模态标记在页面中,模态将显示出来。

              如果您想防止这种情况发生,请使用选项show: false 来初始化模态。像这样的东西: $('.modal').modal({ show: false })

              【讨论】:

                【解决方案15】:

                2020 年更新 - 引导程序 5

                基于先前答案的出色响应,在没有 jQuery 的 Bootstrap 5 中,这已经奏效了;

                document.querySelector('[data-target="#exampleModal"]').click();
                

                完整的sn-p:

                window.onload = () => {
                  document.querySelector('[data-target="#exampleModal"]').click();
                }
                <script src="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/js/bootstrap.min.js"></script>
                <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
                <link href="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/css/bootstrap.min.css" rel="stylesheet" />
                
                
                <div class="container py-3">
                
                  <!-- Button trigger modal -->
                  <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
                    Launch demo modal
                  </button>
                
                  <!-- Modal -->
                  <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                      <div class="modal-content">
                        <div class="modal-header">
                          <h5 class="modal-title" id="exampleModalLabel">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>
                          <button type="button" class="btn btn-primary">Save changes</button>
                        </div>
                      </div>
                    </div>
                  </div>
                
                </div>

                【讨论】:

                • 当前版本的 bootstrap 无法使用的东西 window.onload = () =&gt; { document.querySelector('[data-bs-target="#exampleModal"]').click(); } 目前是正确的
                【解决方案16】:

                您可能希望延迟 jquery.js 以加快页面加载速度。但是,如果 jquery.js 被推迟,$(window).load 可能无法工作。那你可以试试

                setTimeout(function(){$('#myModal').modal('show');},3000);

                它会在页面完全加载后弹出你的模式(包括 jquery)

                【讨论】:

                • 为 DOM 准备好设置超时是最糟糕的做法。如果用户的连接速度很慢怎么办?比它可能需要 10 秒.. 所以永远不要这样做
                【解决方案17】:

                为了避免引导程序被否决并失去其功能,只需创建一个常规启动按钮,为其提供一个 Id,然后使用 jquery '单击它',如下所示: 启动按钮:

                <button type="button" id="btnAnouncement" class="btn btn-primary" data-toggle="modal" data-target="#modalAnouncement">
                  See what´s new!
                </button>
                

                jQuery 触发器:

                $(document).ready(function () {
                  $('#btnAnouncement').click();
                )}
                

                希望对您有所帮助。干杯!

                【讨论】:

                  【解决方案18】:

                  我最近需要在没有 jQuery 的情况下启动 Bootstrap 5 模式,而不是使用 Django 消息单击按钮(例如,在页面加载时)。我就是这样做的:

                  模板/HTML

                  <div class="modal" id="notification">
                    <div class="modal-dialog modal-dialog-centered">
                      <div class="modal-content">
                        <div class="modal-header">
                          <h5 class="modal-title">Notification!</h5>
                          <button type="button" class="btn-close"
                                  data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                          {% for m in messages %}
                            {{ m }}
                          {% endfor %}
                        </div>
                        <div class="modal-footer justify-content-between">
                          <a class="float-none btn btn-secondary" href="{% url 'some_view' %}"
                             type="button">
                            Link/Button A
                          </a>
                          <button type="button" class="btn btn-primary"
                                  data-bs-dismiss="modal">
                            Link/Button B
                          </button>
                        </div>
                      </div>
                    </div>
                  </div>
                  

                  文件或模板中的 JS

                  {% block javascript %}
                    {{ block.super }}
                    <script>
                      var test_modal = new bootstrap.Modal(
                        document.getElementById('notification')
                      )
                      test_modal.show()
                    </script>
                  {% endblock javascript %}
                  

                  这个方法可以在没有 Django 模板的情况下工作;只需使用 HTML 并将 JS 放入文件或 script 元素中,这些元素会在 Bootstrap JS 之后在 body 元素结束之前加载。

                  【讨论】:

                  • 我也用django,但是我的bootstrap没有定义,怎么导入?
                  • 您可以根据他们的文档设置 BS:getbootstrap.com/docs/5.0/getting-started/introduction。在我的例子中,我的 base.html 模板根据需要在其他模板中定义了我的所有 CSS 和 JS,然后我使用 block.super 变量注入额外的 JS。但是你可以通过将 JS 放在一个单独的 JS 文件中来做到这一点,如果你愿意,也可以在页面加载时加载。
                  【解决方案19】:

                  也许已经晚了,但是一旦我无法解决这个订单问题,并且没有显示模态框;我使用了 jquery click();

                  我试过了,效果很好:)

                  创建一个按钮,调用 Modal show > 将该按钮样式设置为 display : none; 示例:

                   
                  
                  
                  $( "#clickme" ).click();
                  <html>
                  <head>
                  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
                  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
                  </head>
                  <body>
                  <input type="button" value="0"  id="clickme" style="display:none;" data-toggle="modal" data-target="#exampleModal" />   
                  <!-- Modal -->
                  <div id="exampleModal" class="modal" 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">Clicked Successfully</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>
                  
                  </body></html>

                  【讨论】:

                    【解决方案20】:

                    你可以使用jQuery来处理这个

                    首先在您的模板上导入

                    <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
                    

                    并使用 jQuery 脚本在模态框上点击 id

                    <script type="text/javascript">
                          $(window).on('load', function() {
                            $('#staticBackdrop').modal('show');
                        });
                    </script>
                    

                    这里是案例显示 django 消息的模式

                    {% if messages %}
                          {% for message in messages %}
                          <div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="false">
                            <div class="modal-dialog">
                              <div class="modal-content">
                                <div class="modal-header">
                                  <h5 class="modal-title" id="staticBackdropLabel">Message</h5>
                                  <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                </div>
                                <div class="modal-body">
                                  {{ message }}
                                </div>
                                <div class="modal-footer">
                                  <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                                </div>
                              </div>
                            </div>
                          </div>
                          {% endfor %}
                            
                        {% endif %}
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2021-12-12
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2019-01-01
                      • 1970-01-01
                      • 2018-05-09
                      相关资源
                      最近更新 更多