Javascript 在页面加载时启动引导模式

Javascript 在页面加载时启动引导模式,javascript,html,twitter-bootstrap,Javascript,Html,Twitter Bootstrap,我一点都不懂javascript。引导程序文档说 通过javascript调用模态:$('#myModal').modal(选项) 我不知道如何在页面加载时调用它。使用引导页面上提供的代码,我可以在元素单击时成功调用模式,但我希望它在页面加载时立即加载。只需将页面加载时要调用的模式包装在文档标题部分的jQueryload事件中,它就会弹出,如下所示: JS $(窗口).on('load',function(){ $('myModal').modal('show'); }); HTML <

我一点都不懂javascript。引导程序文档说

通过javascript调用模态:$('#myModal').modal(选项)


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

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

JS


$(窗口).on('load',function(){
$('myModal').modal('show');
});
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>

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



您不需要
javascript
来显示模式

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

所以

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

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

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

只需添加以下内容-

class="modal show"
工作示例-


开放模态
&时代;
模态头
模态中的一些文本

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

<script src="content/bootstrapJS/jquery-2.1.1.min.js" type="text/javascript"></script>

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

$(窗口)。加载(函数(){
$('myModal').modal('show');
});

在我的例子中,这就是问题所在,希望它对bootstrap 3有所帮助,你只需要通过js初始化模态,如果在页面加载时模态标记在页面中,模态就会显示出来

如果要防止出现这种情况,请在初始化模式的位置使用选项
show:false
。大概是这样的:
$('.modal').modal({show:false})

在我的例子中,添加jQuery以显示模式不起作用:

$(document).ready(function() {
    $('#myModal').modal('show');
});
这似乎是因为模态具有属性aria hidden=“true”:


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

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


注意,我尝试将模态类从
模态淡入
更改为
模态淡入
,但没有成功。另外,将类从
modal fade
更改为
modal show
会使modal无法关闭。

您可能希望将
jquery.js
延迟以加快页面加载。但是,如果延迟
jquery.js
,则
$(窗口).load
可能无法工作。那你可以试试

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


在页面完全加载(包括jquery)后,它将弹出您的模式。

使用Bootstrap3和jquery(2.2和2.3)进行测试

$(窗口).on('load',function()){
$('myModal').modal('show');
});
&时代;
//你的情态标题
//你的模态内容
粪便

您可以尝试此可运行代码-

$(窗口).load(函数()
{
$('myModal').modal('show');
});

开放模态
&时代;
模态头
模态中的一些文本

接近
这里有一个解决方案[没有javascript初始化!] 我找不到一个不使用javascript初始化模式的示例
$(“#myModal”).modal('show')
,因此这里有一个关于如何在页面加载时不使用javascript延迟实现它的建议

  • 使用类和样式编辑模态容器div:
  • 使用类和样式编辑身体:

  • 添加模式背景div

  • 添加脚本

    $(document).ready(function() {
        $('body').css('padding-right', '0px');
        $('body').removeClass('modal-open');
        $('.modal-backdrop').remove();
    
        $('#MyModal').modal('show'); });
    
    将要发生的是,模态的html将在页面加载时加载,没有任何javascript(没有延迟)。此时您无法关闭模式,因此我们使用document.ready脚本在加载所有内容时正确加载模式。实际上,我们将删除自定义代码,然后使用.modal('show')初始化模态


  • 除了user2545728和Reft答案之外,不带javascript,但在

    3个要添加的内容

  • 在.modal类之前的
  • 中包含类
    模态背景的div
    
  • style=“display:block;”“
    到.modal类
  • 淡入
    与.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>
    
    {% block javascript %}
      {{ block.super }}
      <script>
        var test_modal = new bootstrap.Modal(
          document.getElementById('notification')
        )
        test_modal.show()
      </script>
    {% endblock javascript %}
    
    
    欢迎
    你是怎么找到我们的?
    
    您可以通过数据属性激活模式,而无需编写任何JavaScript

    选项“show”(显示)设置为true时显示初始化时的模式:

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

    像其他人提到的那样,使用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>
    
    
    $(“按钮[数据解除=模式]”)。单击(函数(){
    $(.modal.in”).removeClass(“in”).addClass(“fade”).hide();
    $(“.modal background”).remove();
    });
    

    希望这有助于更新2021

    引导程序5 现在引导不再需要jQuery,使用JavaScript显示模式就很容易了

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

    引导程序4 Bootstrap 4的模式标记稍有更改。以下是如何在页面加载时打开模式,以及
    <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>
    
    var myModal = new bootstrap.Modal(document.getElementById('myModal'), {})
    myModal.toggle()
    
    $(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>
    
    <button type="button" id="btnAnouncement" class="btn btn-primary" data-toggle="modal" data-target="#modalAnouncement">
      See what´s new!
    </button>
    
    $(document).ready(function () {
      $('#btnAnouncement').click();
    )}
    
    <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>
    
    {% block javascript %}
      {{ block.super }}
      <script>
        var test_modal = new bootstrap.Modal(
          document.getElementById('notification')
        )
        test_modal.show()
      </script>
    {% endblock javascript %}