Load Bootstrap modal on page load without firing button or using jQuery

谁都会走 提交于 2019-12-13 11:33:07

问题


I'm trying to launch a Bootstrap modal on page load without firing a HTML button or using jQuery.

Here's my code:

<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css">
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>

<div id="my-modal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Title</h4>
            </div>
            <div class="modal-body">
                Hello World!
            </div>
        </div>
    </div> 
</div>

This works when you call

$('#my-modal').modal('show');

But I don't want to call a method or fire a button. Is there a way to launch modal automatically on page load?

Demo: Fiddle


回答1:


Create a CSS class for .modal and add display:block. Also give in class to modal div.

Working Demo

CSS

.modal {
  display:block;
}

HTML

<div id="my-modal" class="modal fade in">

Edit: Somehow default closing function will not work, you will need to add custom script for that.




回答2:


You could utilize the page onload function

<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css">
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>

<body onLoad="$('#my-modal').modal('show');">
    <div id="my-modal" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Title</h4>
                </div>
                <div class="modal-body">
                    Hello World!
                </div>
            </div>
        </div> 
    </div>
</body>



回答3:


I spent some time trying to accommodate this and found that this worked fine for my needs which incorporated a single login page... Obviously this is not the finished article but an excellent base from which I was able to start with. Hope it is of some use to someone...

<body style="background: #555;">
    <!-- Modal -->
    <div class="modal-dialog" style="margin-top: 20%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">Modal title</h4>
            </div>
            <div class="modal-body">
                <p>One fine body…</p>
            </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><!-- /.modal-content -->
    </div>
</body>



回答4:


On Bootstrap 4 --

Add the show class to your modal

<div class="modal fade show" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">

and following css --

.modal {
    display:block;
  }



回答5:


<div id="modal1" class="modal" data-show role="dialog">

data-show help you



来源:https://stackoverflow.com/questions/20794363/load-bootstrap-modal-on-page-load-without-firing-button-or-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!