简体   繁体   English

在页面加载时启动模式

[英]Launch modal on page load

I'm trying to launch a modal when the page loads. 我正在尝试在页面加载时启动模式。 The problem is that I don't have access to the head section. 问题是我无法进入头部。

I have created a modal with a button. 我用按钮创建了一个模态。 If I click that button, the modal shows. 如果单击该按钮,将显示模式。 Now I tried hiding the button, and then simulate a click with javascript. 现在,我尝试隐藏按钮,然后使用javascript模拟点击。 I expect that the modal will show then. 我希望模态会显示出来。 But it doesnt. 但事实并非如此。
Should "document.getElementById("hiddenbutton").click();" 应该为“ document.getElementById(” hiddenbutton“)。click();” not fire the button? 不触发按钮?

<button id="hiddenbutton" type="button"  data-toggle="modal" data-target="#modal_default" style="display: none;"></button>
                <!-- Basic modal -->
            <div id="modal_default" class="modal fade">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h5 class="modal-title">Basic modal</h5>
                        </div>

                        <div class="modal-body">
                            <h6 class="text-semibold">Text in a modal</h6>
                            <p>Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p>

                            <hr>

                            <h6 class="text-semibold">Another paragraph</h6>
                            <p>Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
                            <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p>
                        </div>

                        <div class="modal-footer">
                            <button type="button" class="btn btn-link" data-dismiss="modal">Close</button>
                            <button type="button" class="btn btn-primary">Save changes</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- /basic modal -->


<script type="text/javascript">   

    document.getElementById("hiddenbutton").click();        

thanks 谢谢

It looks like you are using bootstrap, so you can use the $('#modal_default').modal('show') to show the modal. 似乎您正在使用引导程序,因此可以使用$('#modal_default').modal('show')来显示模态。

You don't need access to the head section. 您不需要访问head Just add this javascript code to your page: 只需将此JavaScript代码添加到您的页面即可:

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

Put this script in the head of your page: 将此脚本放在页面的开头:

<script type="text/javascript">

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

</script>

But make sure to import bootstrap.js and jQuery.js in your page before the execution of the above snippet. 但是,请确保在执行上述代码段之前,在页面中导入bootstrap.js和jQuery.js。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM